node_service: fix race between concurrent POST /jobs/{id}#183
Open
JacobZuliani wants to merge 1 commit into
Open
node_service: fix race between concurrent POST /jobs/{id}#183JacobZuliani wants to merge 1 commit into
JacobZuliani wants to merge 1 commit into
Conversation
Two concurrent POST /jobs/{id} to the same node could race past the
`CallHookOnJobStartMiddleware` 409 guard. The guard checks
`SELF["RUNNING"]`, but the mutation that flipped RUNNING=True lived
inside a `wrapped_receive` callback that ran later — on the first
`http.request` event. In the window between "guard checks RUNNING"
and "body arrives and flips RUNNING", a second request could enter,
re-check, and also pass. Both reached `execute`, both drove the
single TCP worker stream, and the second tripped asyncio's
`readexactly() called while another coroutine is already waiting
for incoming data` with a 500 response.
Fix: call `on_job_start(scope)` synchronously at middleware entry,
right after the guard checks. Python's asyncio event loop guarantees
that between a sync `if` check and a sync assignment no other
coroutine runs, so a second request now sees RUNNING=True and 409s.
Removed the `wrapped_receive` indirection, dropped the `started`
single-shot flag (no longer needed since we fire exactly once),
and made `on_job_start` a regular def (it had no awaits).
Existing cleanup still covers the early-client-disconnect case:
`handle_errors`'s `disconnected_mid_assign` branch resets RUNNING
and `current_job` if the client drops before `job_watcher_task`
starts, same as before.
Also updates .cursor/skills/burla-deep-dive/job-lifecycle.md to
reflect the new (simpler) flow.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Two concurrent
POST /jobs/{id}requests to the same node could race past theCallHookOnJobStartMiddleware409 guard. The middleware checkedSELF["RUNNING"]synchronously, but the mutation that flippedRUNNING=Truelived inside awrapped_receivecallback that ran later — on the firsthttp.requestevent. In the window between "guard readsRUNNING=False" and "body arrives and flipsRUNNING=True", a second request could enter, re-check, and also pass. Both reachedexecute, both drove the single TCP worker stream, and the second tripped asyncio'sreadexactly() called while another coroutine is already waiting for incoming datawith a 500.UX impact
Any user running two
remote_parallel_mapcalls at once against the same cluster can hit this — especially whenmax_parallelismis high enough thatmain_serviceselects both onto the same node. The second rpm returns500 Failed to assign <node>, and the first job is usually corrupted too (worker stream is now in an undefined state).Surfaced during test suite work in PR #179; the concurrent-jobs scenario deliberately staggers by 2s to avoid triggering it.
Fix
Move the
SELFstate flip from thewrapped_receivecallback into the middleware entry itself, right after the guard checks pass. Python's asyncio event loop guarantees no other coroutine runs between a sync check and a sync assignment, so a second concurrent request now correctly seesRUNNING=Trueand 409s.Details:
on_job_startbecomes a regulardef(it had noawaits, just oneasyncio.create_taskfor the fire-and-forget Firestore write).wrapped_receiveindirection is gone — along with thestartedsingle-shot flag, since we fire exactly once on middleware entry.handle_errors'sdisconnected_mid_assignbranch still resetsRUNNING=Falseandcurrent_job=Noneif the client drops beforejob_watcher_taskstarts..cursor/skills/burla-deep-dive/job-lifecycle.mdupdated to reflect the simpler flow.Two-file, 34-line diff. No new behavior — tightens an existing race.