Skip to content

fix(sandbox): cancel sibling env resolvers when one fails - #4195

Merged
seratch merged 3 commits into
openai:mainfrom
PranavMishra28:fix/env-resolve-cancel-siblings
Aug 5, 2026
Merged

seratch merged 3 commits into
openai:mainfrom
PranavMishra28:fix/env-resolve-cancel-siblings

Conversation

@PranavMishra28

Copy link
Copy Markdown
Contributor

Problem

Environment.resolve() fanned out with a bare asyncio.gather, so the first failing resolver returned and left the others running.

That matters here specifically because EnvValue is 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/ and voice/ 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:

resolve() raised: secret backend rejected the request
sibling started  : True
sibling cancelled: False
sibling finished : False
still-live tasks : [<Task pending coro=<SlowSecret.resolve() ...>]

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:97 and memory/manager.py:187 fan out internal coroutines only (mkdir layout creation, storage writes).
  • sandboxes/docker.py:1265 already passes return_exceptions=True.
  • materialization.py:67 is covered by its outer cancel-and-drain block, as you noted.
  • voice/result.py is 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 False on the cancelled flag) and asserts no task outlives resolve().
  • test_environment_resolve_still_returns_every_value — pins that the success path still returns every key across the str, EnvValue and EnvEntry shapes, so the cancel path did not change the mapping.
  • Full gate green: ruff, both mypy and pyright, 6481 tests.

Developed with Claude Code; reviewed and tested by Pranav before marking ready for review.

`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 seratch left a comment

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.

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.
@PranavMishra28

Copy link
Copy Markdown
Contributor Author

both done, and the first one was a real hole rather than a style nit.

the blocking resolver waited on a fresh asyncio.Event() that nothing ever set, so the only reason the suite stayed clean was that the assertion happened to pass. run it against base and that task strands for the rest of the session. it now waits on a test-owned release signal, and the test sets that signal in a finally, so the failure path drains it too:

    finally:
        _BlockingEnvValue._release.set()
        with contextlib.suppress(TimeoutError):
            await asyncio.wait_for(_BlockingEnvValue._finished.wait(), timeout=1)

_finished is an Event now rather than a bool, so the success path awaits it instead of peeking at it, which is what actually proves the unwind completed.

all_tasks() assertion is gone. you're right that the two resolver signals are the precise invariant; the global one would also have been sensitive to unrelated tasks under xdist, which is a flake I'd have shipped.

checked against db84dca5~1: still fails on assert _BlockingEnvValue._cancelled, and no dangling-task warning, so the test is honest in both directions now.

gate green, 6481 tests, both mypy and pyright.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread tests/sandbox/test_manifest.py Resolved
@PranavMishra28

Copy link
Copy Markdown
Contributor Author

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.
@seratch seratch modified the milestones: 0.19.x, 0.20.x Aug 5, 2026
@seratch
seratch enabled auto-merge (squash) August 5, 2026 04:34
@seratch
seratch merged commit d270dac into openai:main Aug 5, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants