Skip to content

fix(swebench): propagate verify cancellation from run_instance - #6236

Open
ananthsub wants to merge 1 commit into
mainfrom
ananthsub/swebench-run-instance-cancel
Open

ananthsub wants to merge 1 commit into
mainfrom
ananthsub/swebench-run-instance-cancel

Conversation

@ananthsub

@ananthsub ananthsub commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

When a swebench verify is cancelled, this PR makes the cancellation reach the caller. Today the verify returns a masked "did not complete" result instead, and an episode session saves that result and replays it on retry.

The swebench resources server grades a patch by calling run_instance in resources_servers/swebench/swebench_patches.py. That function stops the verification sandbox and closes its log file in a finally block, and it returned its result from inside that block. A return inside finally discards whatever exception was propagating, which includes asyncio.CancelledError. The return dates from #2421.

#6410 made AsyncSandbox.stop shielded and bounded in time, so the stop now finishes while the request is being cancelled. That made the swallowed cancellation reachable under the server middleware:

  • Before fix(sandbox): shield and bound AsyncSandbox.stop and the other releases #6410. The middleware runs each request in an anyio cancel scope, and a client that disconnects cancels it. The unshielded stop in the finally block then raised CancelledError again, which escaped before the return. So the cancellation reached the caller, by accident.
  • After fix(sandbox): shield and bound AsyncSandbox.stop and the other releases #6410. The stop finishes inside its shield, nothing raises the cancellation again, and the return swallows it. run_instance returns completed: False, and _verify, which has no await after it, builds a masked response from that result.
  • The masked response is saved. For an episode session, verify_task_sandbox_session stores the response as the session's verification. A client that disconnects during evaluation and retries gets that masked result replayed instead of a grade, so the sample is lost.

A plain task.cancel() was swallowed the same way, before and after #6410.

This PR makes two changes in run_instance:

  • It returns its result after the finally block, so a cancellation propagates to the caller. When nothing is cancelled, it returns the same result as before.
  • It closes the logger in an inner finally. The shield in AsyncSandbox.stop holds off an anyio cancel scope, not a second task.cancel() of the same task, as its docstring says. A second cancel during the stop makes cleanup raise, and the inner finally still closes the log file.

DockerContainer.cleanup already catches Exception, so a failed sandbox stop, including one that runs out of time, still does not escape run_instance. The only exceptions that now propagate are BaseException subclasses such as CancelledError.

No issue was filed: a reviewer found the bug while reviewing #6230, which moves the harness logs of each verify into a temporary folder. That PR touches the top of run_instance and this one touches the end, so they overlap only in context.

Validation

  • New tests in resources_servers/swebench/tests/test_app.py run run_instance on a real AsyncSandbox over the fake provider from nemo_gym.sandbox.testing, so the stop is shielded and bounded as in production. The provider's evaluation script runs until it is cancelled, and its close awaits before it finishes. The tests check that:
    • Cancelling the task with task.cancel() makes awaiting it raise CancelledError, after the sandbox is stopped.
    • Cancelling an anyio cancel scope around the call makes run_instance raise instead of returning a result, after the sandbox is stopped.
    • A second task.cancel() that arrives while the sandbox stop is running still closes the log file.
    • In every case, the log file handler is closed and detached from its logger.
  • All of those tests fail with main's swebench_patches.py. Removing only the inner finally around close_logger fails only the second-cancel test.
  • pytest resources_servers/swebench/tests/ in a venv with swebench==4.1.0.
  • pre-commit run --files on the changed files.
  • DCO sign-off on the commit.

Rollouts

N/A. The change affects only what run_instance does when cancelled. On the normal path the function returns the same dictionary from the same values, and the existing failure-only grading tests run run_instance end to end on that path.

Compatibility and benchmark impact

None for verifies that are not cancelled. A cancelled verify now ends with CancelledError instead of returning a masked result, so an episode session no longer saves that result, and a retry grades the patch.

@copy-pr-bot

copy-pr-bot Bot commented Nov 16, 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.

@ananthsub ananthsub added the area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers label Nov 16, 2026
@ananthsub
ananthsub marked this pull request as ready for review November 16, 2026 18:16
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team November 16, 2026 18:17

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHIP

LGTM. No concerns found.

I reviewed all three changed files and traced cleanup through verification masking and session teardown retries. The cancellation handling and normal grading contract are consistent with the added and existing tests.

Static review only. I did not execute tests.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex. This was a full review. Request a full review with /review reset=true.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer labels Nov 16, 2026
@yaoyu-33
yaoyu-33 requested a review from prokotg November 16, 2026 18:35
@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Nov 17, 2026
@ananthsub
ananthsub requested review from init-nikhil and removed request for prokotg November 18, 2026 18:10
@ananthsub ananthsub changed the title fix(swebench): let a cancelled verify end cancelled fix(swebench): propagate verify cancellation and always run sandbox cleanup Nov 18, 2026
@github-actions github-actions Bot removed the sla:review-overdue Review response is over the one-business-day SLA label Nov 18, 2026
@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Nov 19, 2026
Comment thread resources_servers/swebench/app.py Outdated
# The verifier session then stops the sandbox again.
try:
await self._inner_container.stop()
with anyio.fail_after(STOP_TIMEOUT_S, shield=True):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With #6410 adding cancellation protection for AsyncSandbox.stop() plus a timeout, do we still need the extra cancellation shield and timeout here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not needed. I dropped the shield and the time limit from cleanup, since #6410 covers both, and rebased onto main in 2544d7e. cleanup is back to main's version.

The run_instance change stays, and #6410 makes it matter more. Before #6410, the unshielded stop raised the middleware's cancellation again, and that escaped the finally before the return. Now the stop finishes inside its shield, so the return in finally swallows the cancellation. The verify then returns a masked result, and an episode session saves it and replays it on retry. The description has the details.

@github-actions github-actions Bot removed the sla:review-overdue Review response is over the one-business-day SLA label Nov 20, 2026
run_instance returned its result from inside its `finally`, which swallowed a cancellation.
Since AsyncSandbox.stop is shielded, the stop now finishes under a cancelled request,
so nothing after it raises the cancellation again, and a verify cut short by a client
disconnect returned a masked "did not complete" result that an episode session saved.

The result is now returned after the `finally`, and the logger is closed in an inner
`finally`, so a second task cancel during the stop still closes the log file.

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub
ananthsub force-pushed the ananthsub/swebench-run-instance-cancel branch from 3f4dee7 to 2544d7e Compare November 20, 2026 19:34
@ananthsub ananthsub changed the title fix(swebench): propagate verify cancellation and always run sandbox cleanup fix(swebench): propagate verify cancellation from run_instance Nov 20, 2026
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team November 20, 2026 19:35
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the risk:mid Autobot PR risk: mid label Nov 20, 2026
@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer risk:mid Autobot PR risk: mid 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.

3 participants