Skip to content

feat: capture cancelled model calls - #2794

Merged
cmunley1 merged 4 commits into
NVIDIA-NeMo:mainfrom
elisam0:elisam0/capture-cancelled-model-calls
Sep 3, 2026
Merged

cmunley1 merged 4 commits into
NVIDIA-NeMo:mainfrom
elisam0:elisam0/capture-cancelled-model-calls

Conversation

@elisam0

@elisam0 elisam0 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Captures model-call records when a client disconnects or cancels an in-flight request handled by
ModelServerCaptureMiddleware.

Before this change, the middleware only recorded failures caught by except Exception. On Python 3.8+,
asyncio.CancelledError inherits from BaseException, so cancellations raised by the ASGI layer bypassed the
capture path entirely. The result was that client-side timeout/cancellation events could be missing from
model_calls/*.capture.jsonl, making a cancelled request look indistinguishable from a request that never happened.

This PR updates the middleware to catch asyncio.CancelledError alongside normal exceptions, classify it as
error_category: "cancelled", write the capture record, and then re-raise so cancellation semantics are preserved.

It also adds unit coverage that externally cancels an in-flight middleware task and verifies that the raw capture store
contains a cancelled record with no response or status code.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format/copyright pass).
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

@copy-pr-bot

copy-pr-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

Signed-off-by: Elisa Martinez Abad <[email protected]>
@elisam0
elisam0 force-pushed the elisam0/capture-cancelled-model-calls branch from 1b88ab0 to 550139a Compare August 26, 2026 15:22
@elisam0
elisam0 marked this pull request as ready for review August 26, 2026 15:23
@elisam0

elisam0 commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

/claude review

@claude

claude Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

SHIP — LGTM, no reliability concerns.

The change makes _CaptureMiddleware treat asyncio.CancelledError as a first-class capture case rather than letting it escape uncaptured, and classifies it as error_category="cancelled". Reviewed for the ways this could go wrong:

  • Re-raise preserved (base_responses_api_model.py:1236): the raise at the end of the handler re-propagates the CancelledError, so cancellation still unwinds the task correctly — the middleware records the exchange but does not swallow the cancel. No leaked/undead tasks.
  • BaseException correctness: CancelledError is a BaseException (not Exception) since 3.8, so both the except (Exception, asyncio.CancelledError) widening and the explicit isinstance branch in _classify_exception are the right way to catch it. Ordering before the TimeoutError branch is fine since they're disjoint types.
  • Cleanup-on-cancel: the await asyncio.to_thread(_record, ...) and await _flush_deferred_response() inside the handler run after the single cancellation has been delivered; standard cleanup pattern, and the _record write is already guarded by its own try/except+warning.
  • Downstream: "cancelled" flows through rollout_collection.py purely as free-form metadata, and is already an accepted status/outcome literal in rollout_observability.py. No scoring/reward math keys off this string, so no risk of corrupting scores.
  • Test genuinely exercises mid-flight cancellation (app blocked in sleep(3600), cancelled after body read) and asserts capture + null response + error_category=="cancelled" + reraise. Real behavior, not a pass-through.

@github-actions github-actions Bot added the sla:triage-overdue Review assignment is over the one-business-day SLA label Aug 27, 2026
@github-actions github-actions Bot added sla:review-overdue Review response is over the one-business-day SLA and removed sla:triage-overdue Review assignment is over the one-business-day SLA labels Aug 31, 2026
@cmunley1

cmunley1 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

/ok to test 609b06f

@cmunley1
cmunley1 merged commit 33e5ff7 into NVIDIA-NeMo:main Sep 3, 2026
7 checks passed
@cmunley1

cmunley1 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

the await asyncio.to_thread(_record, ...) in the cancel handler is itself cancellable, so a second cancellation (server shutdown, an enclosing asyncio.timeout) drops the record we're trying to save. Do you think we should wrap in asyncio.shield or just call _record sync here?

jkyi-nvidia pushed a commit that referenced this pull request Sep 11, 2026
## What does this PR do?

Captures model-call records when a client disconnects or cancels an
in-flight request handled by
`ModelServerCaptureMiddleware`.

Before this change, the middleware only recorded failures caught by
`except Exception`. On Python 3.8+,
`asyncio.CancelledError` inherits from `BaseException`, so cancellations
raised by the ASGI layer bypassed the
capture path entirely. The result was that client-side
timeout/cancellation events could be missing from
`model_calls/*.capture.jsonl`, making a cancelled request look
indistinguishable from a request that never happened.

This PR updates the middleware to catch `asyncio.CancelledError`
alongside normal exceptions, classify it as
`error_category: "cancelled"`, write the capture record, and then
re-raise so cancellation semantics are preserved.

It also adds unit coverage that externally cancels an in-flight
middleware task and verifies that the raw capture store
contains a cancelled record with no response or status code.

## Checklist

- [x] I have read the [contributing
guidelines](https://docs.nvidia.com/nemo/gym/latest/contribute/development-setup).
- [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] Pre-commit checks pass locally (`pre-commit run --all-files`) (so
CI lint/format/copyright pass).
- [x] All commits have DCO sign-off (`git commit -s`) (so the DCO check
passes).

Signed-off-by: Elisa Martinez Abad <[email protected]>
Co-authored-by: Christian Munley <[email protected]>
Signed-off-by: Junkeun Yi <[email protected]>

This branch was previously deployed

1 inactive deployment
public — 12ee6bd5 Deployed Sep 3, 2026 by copy-pr-bot[bot] via release / finalize / notify #2470
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sla:review-overdue Review response is over the one-business-day SLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants