Repository navigation
fix(capture): don't let cancellation strand a queued model-call write - #4092
Merged
Merged
Conversation
The success path awaited the capture write unshielded while catching only Exception, so a cancellation arriving before the thread-pool executor picked up the job dropped the row with no log line and no incomplete marker. The error path already catches CancelledError; this matches it. Signed-off-by: Wojciech Prazuch <[email protected]>
wprazuch
requested review from
Glorf and
ananthsub
and removed request for
Glorf
October 6, 2026 10:32
Contributor
|
/claude review |
Contributor
|
SHIP — no reliability concerns. Single-line change in Verified:
LGTM. |
ritaneves
self-requested a review
October 6, 2026 12:51
ritaneves
approved these changes
Oct 6, 2026
Contributor
Author
|
/ok to test ee61e2c |
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/37470278649 |
7 tasks done
elisam0
pushed a commit
to elisam0/Gym
that referenced
this pull request
Oct 15, 2026
…a rollout (NVIDIA-NeMo#4593) ## What does this PR do? A model-call capture write can land on disk *after* the rollout record is snapshotted. The row is then present in the capture file but missing from `ng_model_call_capture`, so the rollout is flagged `missing_captured_call` / `trajectory_capture_mismatch` even though nothing about the eval went wrong. This is the race NVIDIA-NeMo#4092 left open. That PR shielded the capture write so cancellation can no longer discard it, and a shielded write runs detached — which is exactly the ordering problem fixed here. The write is now claim-tracked: - the model server writes a per-call pending marker before queueing the write and clears it when the write returns; - rollout collection waits for a rollout's outstanding claims to clear before merging capture into the record (bounded, default 10s, polled every 50ms, returns immediately when nothing is outstanding); - a claim still outstanding at the timeout is recorded as a `model_call_capture_pending` observation gap instead of vanishing. `model_call_capture_pending` is deliberately **not** added to `_INCOMPLETE_MODEL_CALL_GAPS`. A claimed write that never landed is real data loss and should stay unhealthy, not be reclassified as unobserved. Context: full AutomationBench runs currently report 1.7-18% of rollouts missing a captured model call while the scores themselves are unaffected, which fails a 0.1% health gate downstream. ## Validation Run against the final diff (branch merged with `main` first): ``` pytest tests/unit_tests/test_base_responses_api_model.py # 111 passed pytest tests/unit_tests/test_rollout_collection.py \ tests/unit_tests/test_rollout_health.py \ tests/unit_tests/test_rollout_observability.py \ tests/unit_tests/test_rollout_correlation.py # 587 passed pre-commit run --all-files # passed ``` Six new cases in `tests/unit_tests/test_base_responses_api_model.py` cover: claims scoped to one rollout, an unsafe `model_call_id` sanitised into the marker filename, the barrier waiting for a detached write, the barrier giving up on a stranded claim, the merge flagging a claim that never landed, and stale claims being cleared with the captures. The detached-write case asserts the row is absent *before* the barrier and present after, so it fails without this change. ## Rollouts N/A. No environment or agent behaviour changes — this only affects capture bookkeeping on the collection path. Rewards, responses and rollout outputs are untouched. ## Compatibility and benchmark impact No score impact. Adds a bounded wait before merging capture for a rollout; the common path is a single check with nothing outstanding. Adds one new observation gap code, `model_call_capture_pending`. ## Checklist - [x] I have read the [contributing guidelines](https://github.com/NVIDIA-NeMo/Gym/blob/main/CONTRIBUTING.md). - [x] The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs. - [x] Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable). - [x] Documentation added or updated, or N/A with justification. - [x] Pre-commit checks pass locally (`pre-commit run --all-files`) (so CI lint/format checks pass). - [x] New source files include the required Apache-2.0 SPDX header. - [x] All commits have DCO sign-off (`git commit -s`) (so the DCO check passes). --------- Signed-off-by: Wojciech Prazuch <[email protected]>
This branch was previously deployed
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.
Problem
_CaptureMiddlewareawaits the capture write unshielded on the success path while catching onlyException:A cancellation arriving after that await but before the thread-pool executor picks up the job drops the model-call row with no log line, no
.capture.incompletemarker, and no trace anywhere. The error path a few lines above already catchesCancelledError; only the success path does not.The symptom is
trajectory_capture_mismatch/missing_captured_call: the agent'sng_trajectoryreferences aresponse_idthatng_model_call_capturenever recorded.Evidence
Measured on AutomationBench, 600 tasks per run, oci-hsg, by instrumenting four probe points around the finalization and the writer.
CAPTURE_FINALIZE(finalizations entered)CAPTURE_ATTEMPT(writer entered)CAPTURE_WROTE(writer returned)FINALIZE−ATTEMPTCAPTURE_FINALIZE_CANCELLEDmissing_captured_callfindingsThree things this pins down:
ATTEMPTequalsWROTEin both runs — every writer that started, finished. Theflock+fsyncpath inCaptureStore.recordnever loses a row.FINALIZE−ATTEMPTgap is 22, and that run had exactly 22missing_captured_callfindings. No capture-failure warnings were logged, so these were not exceptions.Cancellation on this path is constant (~4000 per 600-task run), but only ~0.5% of cancellations lose data, because
asyncio.to_threadcannot cancel a thread that is already running. Only those landing in the pre-execution window lose the row.Before this, the rate was stable at 5.2–6.3% across four runs that varied checkpoint, serving stack and Gym pin — including a pin carrying #3969, #3970, #3972 and #3974, which address a different capture failure and do not move this one.
Residual: a different mechanism, now isolated
Three rollouts still carry a finding after the fix, down from 22. They are not the same failure, and the distinction is measurable rather than asserted.
Grepping every on-disk
*.capture.jsonlin the run for the flaggedresponse_ids:So before the fix the rows were overwhelmingly never written at all; after it, every flagged row is on disk. What remains is a late write — the row reaches the capture file after the rollout record has been snapshotted, so it is absent from
ng_model_call_capturewhile present on disk.That is a separate race with a separate fix, out of scope here. This change closes the "never written" class completely.
Testing
pytest tests/unit_tests/test_base_responses_api_model.py— 105 passed.No unit test is added.
asyncio.to_threaddoes not cancel an already-running thread, so the defect only reproduces in the window before the executor starts the job, which I could not schedule deterministically. A test that cannot be made to fail without the fix would be vacuous. The two 600-task runs above are the verification instead.