You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(shared): account for an interrupted dequeue before draining a worker - #45551
makeDrainableWorker promises that drain resolves after its scope closes, but an interrupt that lands just after TxQueue.take commits and before the item's ensuring decrement is installed loses the item. The item is no longer in the queue, so the close finalizer does not uncount it, and it never runs, so it does not uncount itself. outstanding stays at 1 and every later drain waits forever. PR refresh, agent-awareness publishing, and storage cleanup all await this shared drain.
Deterministic repro on origin/main (7a00a2bc735a2f00eeb5ba1e9f495edd92a72e86): run a worker with a synchronous processor in its own Scope on new Scheduler.MixedScheduler("async", step), where step only pushes onto a manual callback list, with Scheduler.MaxOpsBeforeYield = 20. Flush startup and enqueue one item. Run N pending callbacks, then call Scope.close(scope, Exit.void) and flush. Then fork worker.drain and flush. The new case in packages/shared/src/DrainableWorker.test.ts does this for N = 0..7:
vp test run packages/shared/src/DrainableWorker.test.ts -t "either side of a dequeue"
Observed: with N = 0–3, close and drain both succeed. With N = 4, close succeeds, processed is 0, and drain stays pending (drain: undefined).
Expected: drain completes for every N because the single item was either processed or dropped.
Change
The worker loop body is now Effect.uninterruptible, and only two parts of it are Effect.interruptible: waiting for an item (TxQueue.peek) and running process. After peek sees an item, the worker removes it with TxQueue.take and installs its decrement with no interrupt check in between. Scope close can still interrupt a waiting or running worker, and a running item still uncounts itself. Wrapping only take in an interruptible region would not close the gap, because take's transaction re-checks for interrupts as it returns after the commit. All callers share this worker, so no change is needed at the call sites. The worker has one consumer, so the uninterruptible take never waits on an empty queue. If the close finalizer has already cleared and shut down the queue, take fails instead of blocking.
Scope
Small fix for an obvious bug; no issue needed under CONTRIBUTING.md.
Verification
Base → head: the new case drains after its scope closes on either side of a dequeue fails on 7a00a2bc735a at N = 4 (drain: undefined). The full file passes on head (5/5), including the existing scope-close, failure-isolation, and trace cases. Across the sweep, the test also checks that it saw both outcomes, the item dropped (0 processed) and the item processed (1), without tying either to a particular N. The test uses no timers, sleeps, or polling. It steps the scheduler's callback list directly inside it.effect.
vp run check:changed exited 0 (format, lint, knip, typecheck across 38 packages, and tests for the changed files).
Diff size: 6 lines added and 2 removed in DrainableWorker.ts; the existing processing block keeps its layout.
Scenario proof: scripts/scenarios cannot cover this. The bug is a scheduler-interleaving race in a server-side shared utility with no UI, and 15_testing.md excludes timing regressions from scenarios. As that page specifies for server-only fixes, the proof is the regression test plus a runtime transcript.
Not shown: a live server run that hits the race on its own (it needs an interrupt in a window of a few operations), or a downstream caller (PR refresh, agent awareness, storage cleanup) hanging end to end.
Authored by Claude Opus 5.5 (Model Hub); reviewed by GPT-6.2 Sol (Codex).
Not shown
before-after-transcript.md:65 — The linked transcript names an earlier implementation (92bf1c7da6aa), and its check log ends at 843cad7bf82c. Refresh the transcript against b0556498e51b with all eight scheduler steps and the matching check summary, or label the linked output as pre-final evidence.
Macroscope's review found this PR approvable — The change protects worker bookkeeping from interruption between dequeuing an item and installing its cleanup. Item processing remains interruptible, with no change to product defaults or the operations performed by callers.
You can add or adjust custom eligibility rules. Learn more.
No actionable comments were generated in the recent review.
Walkthrough
The worker now peeks before taking an item and applies interruption boundaries to queue access, item processing, and its loop. A controlled-scheduler test checks that scope closure and draining complete across callback schedules.
Changes: Worker draining
Layer / File(s)
Summary
Queue access and drain completion packages/shared/src/DrainableWorker.ts, packages/shared/src/DrainableWorker.test.ts
The worker peeks before taking an item, makes queue access and item processing interruptible, and makes the worker loop uninterruptible. The test checks scope closure and draining across callback schedules.
This change makes the drainable worker's dequeue and decrement setup uninterruptible, so closing the scope no longer leaves later drain calls pending. No actionable defect was found, and a new test covers the scope-close timing across eight callback schedules. It looks ready to merge.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: the worker now accounts for an interrupt between dequeue and decrement. It matches the raw summary (uninterruptible worker loop, peek-then-take) and the PR objectives.…
Description check
The description follows the template sections (Problem, Change, Scope, Verification) and fills each with specific content. It gives a deterministic repro, a clear change rationale, scope justification…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the main change: the worker now accounts for an interrupt between dequeue and decrement. It matches the raw summary (uninterruptible worker loop, peek-then-take) and the PR objectives. The title is specific and uses the conventional commit form.
Full details: Description check
Explanation
The description follows the template sections (Problem, Change, Scope, Verification) and fills each with specific content. It gives a deterministic repro, a clear change rationale, scope justification for a small fix without an issue, and detailed verification with commands and observed results. It also states what was not shown. The reviewer note flags that the linked transcript refers to an earlier implementation and an earlier check log, so the evidence is stale. That is a verification gap to fix, but the description is mostly complete. The template's agent-model line is present (Claude Opus 5.5, reviewed by GPT-6.2 Sol).
Resolution
Refresh the linked transcript (gist) against the final head revision b0556498e51b, with all eight scheduler steps and the matching check summary. Alternatively, label the linked output as pre-final evidence. The reviewer note asks for this change.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Reviewing files that changed from the base of the PR and between cf1f69c and e773b52.
Files selected for processing: 2
packages/shared/src/DrainableWorker.test.ts
packages/shared/src/DrainableWorker.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
size:XS0-9 changed lines (additions + deletions).vouch:trustedAuthor is trusted by repo permissions or the VOUCHED list.
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
makeDrainableWorkerpromises thatdrainresolves after its scope closes, but an interrupt that lands just afterTxQueue.takecommits and before the item'sensuringdecrement is installed loses the item. The item is no longer in the queue, so the close finalizer does not uncount it, and it never runs, so it does not uncount itself.outstandingstays at 1 and every laterdrainwaits forever. PR refresh, agent-awareness publishing, and storage cleanup all await this shared drain.Deterministic repro on
origin/main(7a00a2bc735a2f00eeb5ba1e9f495edd92a72e86): run a worker with a synchronous processor in its ownScopeonnew Scheduler.MixedScheduler("async", step), wheresteponly pushes onto a manual callback list, withScheduler.MaxOpsBeforeYield = 20. Flush startup and enqueue one item. Run N pending callbacks, then callScope.close(scope, Exit.void)and flush. Then forkworker.drainand flush. The new case inpackages/shared/src/DrainableWorker.test.tsdoes this for N = 0..7:closeanddrainboth succeed. With N = 4,closesucceeds,processedis 0, anddrainstays pending (drain: undefined).draincompletes for every N because the single item was either processed or dropped.Change
The worker loop body is now
Effect.uninterruptible, and only two parts of it areEffect.interruptible: waiting for an item (TxQueue.peek) and runningprocess. Afterpeeksees an item, the worker removes it withTxQueue.takeand installs its decrement with no interrupt check in between. Scope close can still interrupt a waiting or running worker, and a running item still uncounts itself. Wrapping onlytakein an interruptible region would not close the gap, becausetake's transaction re-checks for interrupts as it returns after the commit. All callers share this worker, so no change is needed at the call sites. The worker has one consumer, so the uninterruptibletakenever waits on an empty queue. If the close finalizer has already cleared and shut down the queue,takefails instead of blocking.Scope
Small fix for an obvious bug; no issue needed under CONTRIBUTING.md.
Verification
drains after its scope closes on either side of a dequeuefails on7a00a2bc735aat N = 4 (drain: undefined). The full file passes on head (5/5), including the existing scope-close, failure-isolation, and trace cases. Across the sweep, the test also checks that it saw both outcomes, the item dropped (0 processed) and the item processed (1), without tying either to a particular N. The test uses no timers, sleeps, or polling. It steps the scheduler's callback list directly insideit.effect.vp run check:changedexited 0 (format, lint, knip, typecheck across 38 packages, and tests for the changed files).nodescript on base and head, plus both test runs): https://gist.github.com/saphid/1fd9e073fb38ecb68d2f0867c56cc4cd. On base, N = 4 printsdrain=pending; on head, every N printsdrain=Success.DrainableWorker.ts; the existing processing block keeps its layout.scripts/scenarioscannot cover this. The bug is a scheduler-interleaving race in a server-side shared utility with no UI, and15_testing.mdexcludes timing regressions from scenarios. As that page specifies for server-only fixes, the proof is the regression test plus a runtime transcript.Not shown: a live server run that hits the race on its own (it needs an interrupt in a window of a few operations), or a downstream caller (PR refresh, agent awareness, storage cleanup) hanging end to end.
Authored by Claude Opus 5.5 (Model Hub); reviewed by GPT-6.2 Sol (Codex).
Not shown
92bf1c7da6aa), and its check log ends at843cad7bf82c. Refresh the transcript againstb0556498e51bwith all eight scheduler steps and the matching check summary, or label the linked output as pre-final evidence.