Repository navigation
fix(sandbox): cancel sibling env resolvers when one fails - #4195
Conversation
`Environment.resolve()` fanned out with a bare `asyncio.gather`, so the first
failing resolver returned and left the others running. `EnvValue` is an extension
point with a subclass registry, so those are user-supplied coroutines that can
reach a secret store or the network: a rejected lookup left sibling secret
fetches in flight after the manifest resolution had already raised, with their
exceptions never retrieved.
Demonstrated before changing anything, with one resolver blocking and a sibling
failing once it is in flight:
resolve() raised: secret backend rejected the request
sibling started : True
sibling cancelled: False
sibling finished : False
still-live tasks : [<Task pending coro=<SlowSecret.resolve() ...>]
Now uses `gather_with_cancel`, the helper already used for the run-path fan-outs,
so the arm is cancelled and drained before the failure propagates. After the
change the sibling reports cancelled and finished and no task outlives the call.
Scoped deliberately to this one call site. The other remaining bare gathers under
`sandbox/` fan out internal coroutines only (`mkdir` layout creation, storage
writes) or already pass `return_exceptions=True`; this is the one that runs
third-party code, which is what makes the orphaning reachable.
Tests: `test_environment_resolve_cancels_siblings_when_one_resolver_fails` fails
without the change (`assert False` on the cancelled flag) and asserts no task
outlives `resolve()`; a companion test pins that the success path still returns
every key, including the str, EnvValue and EnvEntry shapes.
Developed with Claude Code; reviewed and tested by Pranav before marking ready for review.
seratch
left a comment
There was a problem hiding this comment.
Before merging, please make the regression test self-cleaning when run against the base or a future regression. The blocking resolver currently waits forever, so a failed cancellation assertion leaves its task pending. Give it a test-owned release signal, set that signal in a finally block, and await its finished signal. Please also remove the process-global asyncio.all_tasks() assertion; the resolver-specific cancelled and finished signals provide the precise invariant we need.
The blocking resolver waited on a fresh `asyncio.Event()` that nothing ever set, so a failed cancellation assertion left its task pending for the rest of the session. It now waits on a test-owned release signal that the test sets in a `finally`, and `_finished` is an Event the test awaits rather than a bool it peeks at. Also drops the process-global `asyncio.all_tasks()` assertion. The resolver-specific cancelled and finished signals pin the invariant precisely, and the global one would have been sensitive to unrelated tasks under xdist. Verified against the base revision: the test still fails on the intended assertion and its task drains instead of stranding.
|
both done, and the first one was a real hole rather than a style nit. the blocking resolver waited on a fresh finally:
_BlockingEnvValue._release.set()
with contextlib.suppress(TimeoutError):
await asyncio.wait_for(_BlockingEnvValue._finished.wait(), timeout=1)
checked against gate green, 6481 tests, both mypy and pyright. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7ac4291f83
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Resolving CI/CDs and running a few reviews as we speak |
`7ac4291f` reverted `sandbox/manifest.py` to main while rewriting the test, so the PR carried the regression test without the change it was written to pin. CI was therefore exercising the bug with the test that detects it, and failed on `assert _BlockingEnvValue._cancelled` across 3.10, 3.11, 3.13 and 3.14 — which is the test doing its job. Cause was mechanical: the "does this fail on base?" check reverts src/ with `git checkout <base> -- src/`, and that staged the revert, so the follow-up commit picked it up along with the intended test edits. `Environment.resolve()` is back on `gather_with_cancel`. Verified the pushed tree this time rather than only the working tree: `git diff origin/main HEAD -- src/` is non-empty and the full gate is green.
Problem
Environment.resolve()fanned out with a bareasyncio.gather, so the first failing resolver returned and left the others running.That matters here specifically because
EnvValueis an extension point: it is an ABC with a subclass registry (manifest.py:57-111), so these are user-supplied coroutines that can reach a secret store or the network. A rejected lookup left sibling secret fetches in flight after the manifest resolution had already raised, with their exceptions never retrieved.Demonstration
@seratch, on #4142 you said the remaining
sandbox/andvoice/gathers should only change "if we can demonstrate a concrete failure and cover it with a focused regression test." This is that, for one of them.One resolver blocks, a sibling fails once the first is genuinely in flight. Against
main:After the change:
cancelled: True,finished: True,still-live tasks: [].Approach
Uses
gather_with_cancel, the helper already carrying this invariant for the run-path fan-outs, so the arm is cancelled and drained before the failure propagates. One-line change plus the comment explaining why this call site is different.What this deliberately does not change
The other bare gathers under
sandbox/are left alone, and I checked each rather than assuming:memory/storage.py:97andmemory/manager.py:187fan out internal coroutines only (mkdirlayout creation, storage writes).sandboxes/docker.py:1265already passesreturn_exceptions=True.materialization.py:67is covered by its outer cancel-and-drain block, as you noted.voice/result.pyis untouched; different ownership and terminal-event semantics, and I have no concrete failure for it.Environment.resolve()is the one that runs third-party code, which is what makes the orphaning reachable.How tested
test_environment_resolve_cancels_siblings_when_one_resolver_fails— the repro above. Fails without the source change (assert Falseon the cancelled flag) and asserts no task outlivesresolve().test_environment_resolve_still_returns_every_value— pins that the success path still returns every key across thestr,EnvValueandEnvEntryshapes, so the cancel path did not change the mapping.ruff, bothmypyandpyright, 6481 tests.Developed with Claude Code; reviewed and tested by Pranav before marking ready for review.