Repository navigation
Description
The first time a step body reaches a wait, register_hold inserts the hold row and parks the node in one transaction (_hitl.py:608-631). The park is _HOLD_HOLD_NODE_SQL (_hitl.py:315-326): it sets the row pending, clears locked_by_worker and lock_expires_at, and stamps metadata.hold. Its only guard is WHERE id = $1 AND status = 'running'. It doesn't look at claim_epoch, the worker or the attempt, and register_hold ignores how many rows it updated. register_hold has no epoch parameter (_hitl.py:510-526) and WaitHost doesn't carry one (_ctx_wait.py:116-143), even though the step context already has claim_epoch (_ctx.py:139-147).
A body whose lease lapses can keep running (the finalize fence below exists for that case), so it can reach its first ctx.wait_signal after a newer claim has taken the row. It finds no held, delivered or abandoned row for its wait site, mints a fresh hold (_ctx_wait.py:501-519), and the park lands on the row the newer claim is running. The runner's NodeHeldError arm writes nothing to jobs (_runner.py:1636-1651), so the park is the only write. The terminal write handles the same situation: the finalize CAS fences on status, worker, attempt and claim_epoch, and its comment says a status-only CAS "loses to the zombie" (_sql_finalize.py:176-185). The architecture doc says claim_epoch is "fenced on by every terminal/ownership write" (architecture.md:1929-1931), and dropping the lock and re-pending the row is an ownership write.
Any step whose first wait comes after work that can outlive the lock lease has this shape:
class Approval(BaseModel):
ok: bool
async def review(ctx: StepContext) -> str:
await prepare() # long enough for the lease to lapse
await ctx.wait_signal(Approval) # first wait: mints the hold
return "done"
@app.workflow("review")
def review_flow() -> Promise[object]:
return build(step(review, key="review"))Reproduced on ffcb61f against a real Postgres through the fleet claim path (no file under src/ changed between that commit and main at 9b9fa72). Claim 1 (claim epoch 1, 1 second lease) took the row and was paused before its first wait_signal. The lease lapsed, the lock sweep returned the row, and claim 2 (epoch 2) took it and was paused as well. The one shortcut was setting the returned row's scheduled_at to now() so claim 2 didn't wait out the reclaim backoff. Then claim 1's body was let through to its wait. The row's (status, locked_by_worker IS NOT NULL, claim_epoch, metadata.hold) went from ('running', True, 2, None) to ('pending', False, 2, '01a20f08-d7f8-7b00-ad44-5c2a8b4e0f5e'), and that id was the node's only held wf_signals row. Every run that completed gave the same result, including two at the head of PR 755 (f719668). With AND claim_epoch = 1 added to the park, the same sequence leaves claim 2's row running, locked and unstamped.
Claim 2's body is still running on a row that is now pending and unlocked. What happens next comes from reading the code; the reproduction stopped once the park had landed. If it gets as far as a terminal write, the finalize fence no longer matches (status and worker have changed), so its result is dropped. If it reaches the same wait, it finds the stale body's hold and raises NodeHeldError (_ctx_wait.py:415-431), so a deterministic body ends on a consistent held row and nothing is corrupted. Before claim 2 gets that far, the row can be claimed again while claim 2 is still running. An untimed wait leaves scheduled_at alone, so the row is due at once, and on main the fleet dispatch fence has no hold leg (issue 748), so another worker could take the row and run the body next to claim 2. The join mint (_childflow.py:349-363) and the timer mint (_ctx_timer.py:338, which reaches register_hold at _hitl.py:712) run the same statement; only the wait_signal mint was exercised.
This came up in the design work for issue 748. PR 755 gives this statement an optional claim-epoch fence and uses it for the re-entry re-park, but the mint passes None, and the PR description says claim-fencing the fresh mint in register_hold isn't part of it, so the gap predates the PR and stays after it.
Probably wants: register_hold taking the claim's epoch (through WaitHost and the join site, the way PR 755 threads it for the re-park), passing it to the park, and checking the park's row count. A mint that gets fenced out then has to decide what to do with the hold row it already inserted in the same transaction (raise and roll back, or keep the row and leave the node alone), and that choice changes the fresh-mint error path for signal, join and timer waits alike.
Activity
XBeg9 commentedon Nov 24, 2026
This shipped with the workflow engine in
5b383f295(PR 681), which added_HOLD_HOLD_NODE_SQLwith only thestatus = 'running'guard (_hitl.py:319-330). The line history shows the statement hasn't changed since then. The reproduction described above fails the same way at that commit and at9b9fa7244on main. In both runs claim 2's row goes from running and locked to pending, unlocked and stamped with the stale body's hold. The parent66b8f369dhas notaskq.workflowspackage, so there's no earlier fenced version to regress from, even though plain job terminal writes already fenced onclaim_epochby then (PR 369).Related:
None(_hitl.py:740-746), so merging it as it stands won't close this. On main, the fleet fence's missing hold leg from 748 is also what lets the wrongly parked row be claimed again while claim 2 is still running.Snoozebecause their cursors are keyed on the attempt number, which is a separate bug on the same mint path.fix(workflows): harden hold-mint fencing, re-entry unwind, loop echo,…
fix(workflows): harden hold-mint fencing, re-entry unwind, loop echo,…
test(workflows): align synthetic claim_epoch and running state with h…
fix(workflows): fence fresh hold mints on claim_epoch to prevent stal…