Repository navigation
Conversation
Greptile SummaryThis PR fixes silent message loss on WhatsApp socket close by calling Confidence Score: 5/5Safe to merge; the fix is correct and both remaining findings are P2 style suggestions. No P0/P1 issues found. The flush ordering and sequencing logic are correct. The two P2 comments are optional improvements: parallel key flushing to reduce close-path latency, and moving detachConnectionUpdate before the flush for a cleaner shutdown boundary. No files require special attention.
|
| const flushAll = async () => { | ||
| const keys = Array.from(buffers.keys()); | ||
| for (const key of keys) { | ||
| await flushKey(key); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Sequential flush adds close-path latency
flushAll awaits each key one at a time, so total close time is the sum of all onFlush durations. Since each key maps to a different conversation/sender, the per-key flushes are independent and can run in parallel without ordering concerns between them (per-key ordering is already guaranteed by keyChains).
| const flushAll = async () => { | |
| const keys = Array.from(buffers.keys()); | |
| for (const key of keys) { | |
| await flushKey(key); | |
| } | |
| }; | |
| const flushAll = async () => { | |
| const keys = Array.from(buffers.keys()); | |
| await Promise.all(keys.map((key) => flushKey(key))); | |
| }; |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/auto-reply/inbound-debounce.ts
Line: 234-239
Comment:
**Sequential flush adds close-path latency**
`flushAll` awaits each key one at a time, so total close time is the sum of all `onFlush` durations. Since each key maps to a different conversation/sender, the per-key flushes are independent and can run in parallel without ordering concerns between them (per-key ordering is already guaranteed by `keyChains`).
```suggestion
const flushAll = async () => {
const keys = Array.from(buffers.keys());
await Promise.all(keys.map((key) => flushKey(key)));
};
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c4fa24ed78
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const keys = Array.from(buffers.keys()); | ||
| for (const key of keys) { | ||
| await flushKey(key); |
There was a problem hiding this comment.
Keep flushing until buffer map is empty
flushAll snapshots buffers.keys() once and never re-checks the map, so entries enqueued during the flush window are skipped. In this flow, monitorWebInbox.close() calls flushAll() and then closes the socket, and detachMessagesUpsert() does not cancel an already-running async handleMessagesUpsert callback; that callback can still enqueue additional items after the snapshot. Those late items remain buffered and are dropped when the socket closes, so the new shutdown path can still lose debounced inbound messages.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 81be021f5c
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const keys = Array.from(buffers.keys()); | ||
| await Promise.all(keys.map((key) => flushKey(key))); |
There was a problem hiding this comment.
Drain active keyed tasks before returning from flushAll
flushAll only awaits keys that are still present in buffers, but a debounce timer callback removes the buffer before awaiting buffer.task (tracked in keyChains). If close() runs during that window, flushAll can resolve even though a debounced onFlush is still executing, and monitorWebInbox.close() will then close the WhatsApp socket immediately after, causing in-flight reply/sendMedia calls to fail and drop those messages.
Useful? React with 👍 / 👎.
When the WhatsApp connection closes (disconnect, watchdog, or shutdown), messages sitting in the inbound debounce window are silently dropped because the socket is closed before pending timers fire. Add a flushAll() method to the inbound debouncer and call it during the close sequence — after detaching the message listener (preventing new arrivals) but before closing the socket (so replies can still be sent). This ensures debounced messages are processed and replied to even during connection transitions.
- Use Promise.all for parallel flush (keys are independent conversations) - Move detachConnectionUpdate before flushAll for cleaner shutdown boundary - Add flushAll to mock debouncers in feishu, slack, bluebubbles, and plugin-runtime-mock to fix TS2322 type errors - Add flushAll to BlueBubblesDebouncer type definition
81be021 to
48a45ba
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 48a45bad6d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const keys = Array.from(buffers.keys()); | ||
| await Promise.all(keys.map((key) => flushKey(key))); |
There was a problem hiding this comment.
Wait for debouncer to fully quiesce before returning flushAll
flushAll only snapshots buffers.keys() once and awaits flushKey for that snapshot, so it can return while debounce work is still being created or still running. In the new WhatsApp shutdown flow, detachMessagesUpsert() does not cancel already-running handleMessagesUpsert callbacks, so they can enqueue new buffered items after this snapshot; additionally, timer flushes remove a buffer before awaiting its keyed task, so in-flight onFlush work can exist with no buffer entry. Because close() now closes the socket immediately after flushAll, those late/in-flight flushes can still execute against a closed socket and drop messages.
Useful? React with 👍 / 👎.
|
Codex review: needs real behavior proof before merge. Summary Reproducibility: yes. source-reproducible: configure Real behavior proof Next step before merge Security Review findings
Review detailsBest possible solution: Implement a debouncer drain that waits until there are no buffered keys and no active keyed chains before closing WhatsApp, with a focused regression proving Do we have a high-confidence way to reproduce the issue? Yes, source-reproducible: configure Is this the best way to solve the issue? No. A close-path drain is the right direction, but the proposed one-shot buffer snapshot can miss late enqueues and active Full review comments:
Overall correctness: patch is incorrect Acceptance criteria:
What I checked:
Likely related people:
Remaining risk / open question:
Codex review notes: model gpt-5.5, reasoning high; reviewed against a5668efb7aca. |
|
Thanks for the PR and for working on this. We checked the current main branch, and this fix is already in main via #81246, which landed in commit 81a3de1. Handling already-received debounced WhatsApp inbound messages before closing the socket, so replies and media sends from that batch run first, is already covered on main, so the behavior this PR changes is already handled there. Because that has landed, I’m closing this PR as superseded by #81246. Thanks again for the work here. If you think this closure is mistaken and your PR still fixes something meaningfully different on current main, feel free to open a new PR with that explanation. |
Problem
When the WhatsApp connection closes (disconnect, watchdog timeout, or graceful shutdown), messages sitting in the inbound debounce window are silently dropped. This happens because:
close()handlermsg.reply,msg.sendMedia) fail silently on the closed socketThis affects deployments with
messages.inbound.debounceMs > 0configured — rapid consecutive messages from the same sender may be lost during connection transitions.Fix
src/auto-reply/inbound-debounce.ts: Add aflushAll()method that iterates all buffered keys and flushes them using the existingflushKey()logic (which properly clears timers, releases keyed execution slots, and callsonFlush).extensions/whatsapp/src/inbound/monitor.ts: Calldebouncer.flushAll()in theclose()handler, sequenced between:detachMessagesUpsert()— stop accepting new messagesdebouncer.flushAll()— process buffered messages while socket is still alivecloseInboundMonitorSocket(sock)— close the socketThis ordering ensures buffered messages are processed and replies sent before the socket is torn down.
Testing
inbound.test.ts) coverflushKeybehaviorflushAlldelegates toflushKeyper-key, inheriting the same guarantees