Skip to content

fix(platform): hand over a drained job only once its claim ended - #4003

Merged
yannickmonney merged 3 commits into
mainfrom
fix/drain-hand-over-result-guard
Sep 30, 2026
Merged

yannickmonney merged 3 commits into
mainfrom
fix/drain-hand-over-result-guard

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4000. References #3977; broader capacity work remains open.

A draining worker could recreate cancelled or already-completed work because it ignored boss.complete and always queued a successor. The hand-over now validates both results inside one transaction: affected: 1 permits a successor; affected: 0 stands down; malformed or unconfirmed completion rolls back. A null successor coalesces only under the supported short/stately queue policy; other policies or a missing queue roll back. The worker and normal enqueue path share bossDbInTx, and the manual automation register names the new controls.

The correction preserves the original key, priority, payload, delay and transaction order. Agent admission and the recheck queue are unchanged.

Validation:

  • Paired local real-Postgres controls: corrected source 127/127; previous source with the same tests 122/127, failing exactly the five new drain cases. Effects were synthetic and agent/provider execution was held inert.
  • Final reviewed head bd4200dada9b66b587e201b2a23670351745f5fd: all five normal workflows passed. Strict hosted backend integration actually ran 1,225/1,225 checks across 206/206 lanes, including all 11 drain controls, with no skips or failures. One retained ffmpeg prerequisite timeout preceded a single successful normal retry; diagnostics are tracked in CI: make ffmpeg prerequisite timeouts diagnosable #4010.
  • Full platform Typecheck actually passed after the separate ci(platform): give the Type check job a 6 GiB Node heap #4007 heap correction. The final source also passed unit/component/browser tests, E2E, fresh production dependency pruning, image validation 40/40, sandbox runtime 89/89 and smoke 29/29. Existing caches, inherited test skips and advisory image scanning remain disclosed in the final review.
  • Normal squash merge bd25a6acb64b5fce8215f9e888fd19a24ded4291 preserved the exact reviewed fix plus the separately accepted, disjoint Gemini update on main. The combined tree matches the independent source-composition result. These PR checks tested the prior merge base; complete combined candidate validation is required before release.

Final merge acceptance and proof boundaries. Historical failure and paired-control evidence remain in the review comments. No production test or recurring-cycle activation was performed.

Independent review of #3985 at 5c17689: the draining worker's hand-over
(jobs/runner.ts) discarded what boss.complete answered and sent the
successor anyway. pg-boss completes active jobs only, so a claim cancelled
between its fetch and its hand-over, or completed elsewhere meanwhile,
answers { affected: 0 } and still got a successor, which can bring back
withdrawn or finished work once no same-key job remains.

The hand-over now reads both answers inside its one transaction. The claim
is still completed first, and the successor still carries the job's own
key, priority and payload, five seconds out.
- affected 1: this worker's claim ended here, so the successor is sent.
- affected 0: the claim was no longer active (cancelled, completed or
  expired), so nothing is sent and the empty transaction commits. The
  worker logs it, and pg-boss's own completion of the batch, which touches
  active jobs only, changes nothing.
- Any other answer confirms nothing (pg-boss types the response as an
  empty interface), so the hand-over throws and rolls back, and the claim
  takes pg-boss's failure path under its queue's retry policy.
- A refused successor (send answers null) collapses only on a short or
  stately queue, where the job already queued under the key does the work.
  The policy is pg-boss's own record of the queue, read on that path only.
  On any other policy, or a queue it cannot find, the hand-over rolls back.

The transaction adapter is now one helper, bossDbInTx in jobs/enqueue.ts,
used by addJobInTx and the hand-over alike.

Proof: runner.test.ts covers each answer: affected 1, the real zero shape,
six malformed answers, and a refusal on short and stately versus standard,
exclusive, singleton, key_strict_fifo and a missing queue. It also shows
both writes ride the transaction through the shared adapter.
checkWorkerDrainHandOff adds, with the actual worker on real Postgres:
- a claim cancelled between its fetch and its hand-over, and one completed
  elsewhere (each: no successor, the completion answering affected 0);
- a real stately collapse;
- through a scripted boss, since pg-boss does not give these answers
  there: a refused successor on a standard and on an exclusive queue, and
  an unconfirmed completion. Each rolls back: the claim ends failed with
  the reason, not completed, and nothing is sent.
The manual register row says so.

Refs #3977
The Type check job of e6bdde0 died with V8 heap exhaustion in the
platform's tsc (exit 137, about 4 GB), not with a type error. Main's own
run minutes earlier passed, and on the same scoped program this change
adds 0.1% types and 0.08% instantiations. The token cannot re-run a job,
so this empty commit re-runs the checks.

Refs #3977
@yannickmonney

Copy link
Copy Markdown
Contributor Author

CI at 173fe359f: 49 checks pass, 12 are skipped, and Type check fails. The failure is not a type error. The platform's full tsc runs out of V8 heap, and the same happens on other PRs.

  • Two identical failures here. The original head e6bdde08b and the re-run 173fe359f (an empty commit, same tree) both die in @tale/platform#typecheck after about 135 s. The log reads FATAL ERROR: Ineffective mark-compacts near heap limit at about 4,030 MB, with exit 137. That is Node's default old-space limit on the hosted runner.
  • Another PR hits it too. fix(sandbox): pin Gemini CLI to 0.59.0 #4001 changes one TypeScript test file and died the same way at 14:20 (job). Main's own push runs (4d14c55d5, cc325fc5c) pass, and so did fix(platform): fail the image build when its production install fails #4004. Main's full program therefore sits within a few MB of the limit, and small additions tip it over.
  • This change adds almost nothing type-wise. Measured with --extendedDiagnostics on the same scoped program (integration-check.ts plus the jobs files):
    • types +773 (+0.1%);
    • instantiations +1.3k (+0.08%);
    • symbols +3.4k.
  • Everything else is green. That includes Backend integration: 1225/1225 checks passed across 206/206 lanes on the merge ref faabc6140, with all 11 drain checks and all 21 busy-retry checks (job).

I left the type-check setup alone because it is outside this correction. One way to add headroom is to change the platform script to "typecheck": "NODE_OPTIONS=--max-old-space-size=6144 bunx tsc --noEmit", or to set the same environment variable on the Type check job. I can open that as its own PR if you want it.

@yannickmonney
yannickmonney merged commit bd25a6a into main Sep 30, 2026
73 of 74 checks passed
@yannickmonney
yannickmonney deleted the fix/drain-hand-over-result-guard branch September 30, 2026 16:50
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Merged through the normal protected squash path at bd25a6acb64b5fce8215f9e888fd19a24ded4291 after independent review of exact head bd4200dada9b66b587e201b2a23670351745f5fd.

All five normal workflows passed. The backend retry actually executed 1,225/1,225 checks across all 206 lanes, with no skips or failures, including the worker drain controls. The original ffmpeg prerequisite timeout is retained and tracked separately in #4010. Full platform typecheck, unit/component/browser checks, E2E, fresh platform-image production pruning, image validation 40/40, runtime 89/89 and smoke 29/29 passed; existing caches, inherited test skips and the advisory Trivy result remain explicit in the review evidence.

Main advanced with the separately reviewed Gemini update while these checks ran. Independent source composition found no overlap and preserved both complete five-file changes. The actual squash commit has parent ed31183c3913dd05d857b4f17aff6e6db4fc1609 and exactly the expected combined tree 6c8454b10e2e45101c184d1a21641fb24c17581c. Complete combined release-candidate validation remains a separate required step before publication.

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.

Bug: cancelled jobs can be recreated during worker drain handoff

1 participant