Skip to content

fix: add #[must_use] to AbortHandle and clear message queues on disconnect - #459

Merged
jlucaso1 merged 1 commit into
mainfrom
fix/must-use-abort-handle-and-message-queue-cleanup
Mar 28, 2026
Merged

jlucaso1 merged 1 commit into
mainfrom
fix/must-use-abort-handle-and-message-queue-cleanup

Conversation

@jlucaso1

@jlucaso1 jlucaso1 commented Mar 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

Minimal, targeted fixes from the resource management audit — only the changes that provide real value without adding complexity.

  • #[must_use] on AbortHandle: The compiler now warns when a spawned task's handle is silently dropped (which would abort the task). Forces every spawn site to make an explicit choice: .detach() for fire-and-forget, or store the handle for lifecycle management. Zero runtime cost.
  • message_queues.invalidate_all() in cleanup_connection_state(): Drops per-chat message queue senders on disconnect so workers exit via channel close. Without this, stale workers from the previous connection survived reconnects holding outdated signal/crypto state.

Why not TaskTracker?

The original approach (PR #458) added a TaskTracker container for structured task lifecycle management. After thorough review, this was overengineered — the existing cooperative cancellation via connection_generation, shutdown_notifier, and is_shutting_down() already handles task lifecycle correctly (matching WhatsApp Web's AbortController pattern). The TaskTracker added complexity (Arc reference cycles, unbounded handle growth, abort timing issues with 515 reconnect cycles) without improving stability.

Test plan

  • cargo clippy --all --tests — zero warnings
  • cargo test --all --exclude e2e-tests — all pass
  • Verified #[must_use] produces compiler warning on unhandled runtime.spawn() result
  • Verified message_queues.invalidate_all() placement is after is_connected = false (workers see disconnected state on any in-flight sends)

Summary by CodeRabbit

  • Bug Fixes
    • Improved connection cleanup to properly reset per-chat worker states during reconnection, preventing issues from outdated connection state persistence.

…nnect

- Add #[must_use] to AbortHandle so the compiler warns when a spawned
  task's handle is silently dropped (forces explicit .detach())
- Invalidate message_queues in cleanup_connection_state() so stale
  per-chat workers don't survive reconnects with outdated crypto state
@coderabbitai

coderabbitai Bot commented Mar 28, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: d68784c6-018d-445f-a4ce-a723a8f6cda6

📥 Commits

Reviewing files that changed from the base of the PR and between e0a128b and 6d01850.

📒 Files selected for processing (2)
  • src/client.rs
  • wacore/src/runtime.rs

📝 Walkthrough

Walkthrough

This PR adds message queue invalidation to connection cleanup logic and introduces a #[must_use] diagnostic attribute to the AbortHandle struct to warn against accidental task abortion from dropped handles.

Changes

Cohort / File(s) Summary
Connection Cleanup
src/client.rs
Added message_queues.invalidate_all() call in Client::cleanup_connection_state to ensure per-chat worker queues are dropped on reconnect, forcing workers holding stale connection state to exit.
Runtime Diagnostics
wacore/src/runtime.rs
Added #[must_use] attribute to AbortHandle struct with diagnostic message directing users to .detach() for fire-and-forget behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • PR #363: Modifies Client::cleanup_connection_state to reset offline sequence tracking logic, directly touching the same cleanup function.
  • PR #310: Modifies Client::cleanup_connection_state to drain response waiters, overlapping with the cleanup state management.
  • PR #455: Modifies Client::cleanup_connection_state to clear pending retries, intersecting with the same function's lifecycle management.

Poem

🐰 A rabbit's ode to tidy cleanup:
When connections fade, the queues must clear,
No phantom workers lingering here!
And handles dropped without a care,
Now whisper warnings in the air—
Use .detach() if you dare!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately describes both main changes: adding #[must_use] to AbortHandle and clearing message queues on disconnect, matching the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/must-use-abort-handle-and-message-queue-cleanup

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6d01850866

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/client.rs
// Drop per-chat message queue senders so workers exit via channel close.
// Without this, stale workers from the old connection survive reconnects
// holding outdated signal/crypto state.
self.message_queues.invalidate_all();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Avoid dropping chat-queue map before workers finish

Calling self.message_queues.invalidate_all() in cleanup_connection_state removes the sender entry immediately, but existing per-chat workers keep running until their channel drains (MessageHandler loops on rx.recv() in src/handlers/message.rs). If a disconnect happens with buffered messages and reconnect is fast, the next incoming message for that chat creates a second worker/queue while the old worker is still draining, so messages for the same chat can be processed concurrently and out of order (breaking the mailbox guarantee used to preserve Signal session ordering).

Useful? React with 👍 / 👎.

@jlucaso1
jlucaso1 merged commit 72980a6 into main Mar 28, 2026
9 of 10 checks passed
@jlucaso1
jlucaso1 deleted the fix/must-use-abort-handle-and-message-queue-cleanup branch March 28, 2026 22:42
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.

1 participant