Skip to content

fix: cleanup session for pending_order_exists cant-do responses#287

Merged
grunch merged 1 commit into
mainfrom
cleanup-session-pending_order_exists
Sep 2, 2025
Merged

fix: cleanup session for pending_order_exists cant-do responses#287
grunch merged 1 commit into
mainfrom
cleanup-session-pending_order_exists

Conversation

@Catrya

@Catrya Catrya commented Sep 2, 2025

Copy link
Copy Markdown
Member

fix #248

  • Add specific session cleanup for order taking failures
  • When cant-do reason is pending_order_exists, delete session by orderId
  • Prevents phantom orders from appearing in "My Trades" after failed order attempts
  • Separates cleanup logic: requestId for order creation, orderId for order taking

Summary by CodeRabbit

  • Bug Fixes
    • Expanded automatic cleanup to cover cases where a conflicting pending order exists, reducing stuck sessions and duplicate prompts.
    • Enhances overall reliability during order validation by clearing invalid or conflicting sessions sooner, minimizing confusing errors and improving flow continuity.

  - Add specific session cleanup for order taking failures
  - When cant-do reason is pending_order_exists, delete session by orderId
  - Prevents phantom orders from appearing in "My Trades" after failed order attempts
  - Separates cleanup logic: requestId for order creation, orderId for order taking
@coderabbitai

coderabbitai Bot commented Sep 2, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Adds a new cleanup branch in Mostro notifier: when cantDoReason is pendingOrderExists, it deletes the session for the given orderId via sessionNotifierProvider.notifier.deleteSession(orderId). Existing cleanup for outOfRangeSatsAmount remains unchanged. No public API changes.

Changes

Cohort / File(s) Summary
CantDo cleanup enhancement
lib/features/order/notfiers/abstract_mostro_notifier.dart
Adds session cleanup on CantDoReason.pendingOrderExists by calling deleteSession(orderId); retains existing outOfRangeSatsAmount cleanup.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant U as User
  participant MN as MostroNotifier
  participant SN as SessionNotifier

  U->>MN: Take order
  MN-->>MN: Process response
  alt cantDo: pendingOrderExists (new)
    MN->>SN: deleteSession(orderId)
    SN-->>MN: Session deleted
  else cantDo: outOfRangeSatsAmount (existing)
    MN->>SN: deleteSession(orderId)
    SN-->>MN: Session deleted
  end
  MN-->>U: Return cantDo result
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Assessment against linked issues

Objective Addressed Explanation
Prevent ghost/stuck order UI after cant_do pending_order_exists by cleaning related session (#248)

Poem

I nudge the queues with twitching nose,
A pending warren now I close.
One hop—sessions cleared, no tricks,
No ghostly carrots in the mix.
Burrow tidy, orders neat—
Tap-tap, thump-thump: bug retreat! 🥕🐇

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch cleanup-session-pending_order_exists

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
lib/features/order/notfiers/abstract_mostro_notifier.dart (2)

311-314: Also clear persisted messages for the attempted order (prevents stale badges/threads)

If any messages were cached before the cant_do arrives, they can leave residual chat state. Consider aligning this branch with the canceled path by clearing message storage as well.

Apply:

 if (cantDo?.cantDoReason == CantDoReason.pendingOrderExists) {
+  ref.read(mostroStorageProvider).deleteAllMessagesByOrderId(orderId);
   ref.read(sessionNotifierProvider.notifier).deleteSession(orderId);
 }

13-17: Nit: Folder name typo

The path segment “notfiers” looks like a typo of “notifiers.” Consider renaming in a follow-up to avoid future grep/import surprises.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 2645c22 and f0747a5.

📒 Files selected for processing (1)
  • lib/features/order/notfiers/abstract_mostro_notifier.dart (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
{lib/*.dart,lib/!(generated)/**/*.dart}

📄 CodeRabbit inference engine (CLAUDE.md)

{lib/*.dart,lib/!(generated)/**/*.dart}: Use localized strings; replace hardcoded user-facing text with S.of(context).keyName
Prefer const constructors and const where possible
Use latest Flutter/Dart APIs (e.g., withValues() instead of deprecated withOpacity())

Files:

  • lib/features/order/notfiers/abstract_mostro_notifier.dart
{lib/*.dart,lib/!(generated)/**/*.dart,test/**/*.dart}

📄 CodeRabbit inference engine (CLAUDE.md)

{lib/*.dart,lib/!(generated)/**/*.dart,test/**/*.dart}: Check mounted before using BuildContext after await (async gaps)
Remove unused imports and dependencies

Files:

  • lib/features/order/notfiers/abstract_mostro_notifier.dart
**/*.dart

📄 CodeRabbit inference engine (CLAUDE.md)

All code comments, variable names, and function names must be in English

Files:

  • lib/features/order/notfiers/abstract_mostro_notifier.dart
🧠 Learnings (1)
📓 Common learnings
Learnt from: chebizarro
PR: MostroP2P/mobile#74
File: lib/services/mostro_service.dart:97-103
Timestamp: 2025-05-06T15:49:55.079Z
Learning: In the Mostro protocol, an order cannot be canceled unless it has a valid orderId, so null checks on orderId during cancel operations are unnecessary.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (3)
lib/features/order/notfiers/abstract_mostro_notifier.dart (3)

311-314: Targeted cleanup for pendingOrderExists — LGTM

Deleting the session by orderId on cant_do: pendingOrderExists matches the PR objective and should remove the phantom entry from “My Trades.” Good separation from requestId-based cleanup.


311-314: Double-check we’re deleting the “attempted” order’s session

In this notifier the orderId corresponds to the order being taken (the one that failed). That’s the session we want to drop—not the already-pending order that triggered the cant_do. Please confirm events for pendingOrderExists here are indeed tied to the attempted order’s id (current notifier) in all transports.


18-19: Potential LateInitializationError: session may be uninitialized

late Session session; is assigned only when oldSession != null. Several branches dereference session (saveSession(session), session.role) and could throw if no previous session exists. If construction without a preexisting session is possible in any flow, this is a crash risk.

Options:

  • Ensure a session is always created before this notifier is constructed.
  • Or initialize a default session in the constructor when oldSession is null.
  • Or remove the field and always fetch from sessionNotifierProvider at use sites.
    Do you want a patch for one of these approaches?

Also applies to: 32-37, 128-129, 200-207, 228-236, 338-345

@grunch grunch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@grunch grunch merged commit 6f710e8 into main Sep 2, 2025
2 checks passed
@grunch grunch deleted the cleanup-session-pending_order_exists branch September 2, 2025 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when taking a new order if I already have one waiting

2 participants