Repository navigation
fix(run): cancel sibling guardrail tasks when one raises - #3239
Conversation
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
seratch
left a comment
There was a problem hiding this comment.
can you resolve the conflicts?
bd17e44 to
8a29e3b
Compare
|
Rebased onto current main; conflicts resolved (combined with #3187's tripwire-branch await). Tests + lint clean. |
8a29e3b to
5f2fe20
Compare
|
This PR is stale because it has been open for 10 days with no activity. |
run_input_guardrails / run_output_guardrails iterate guardrail tasks via asyncio.as_completed and only cancel siblings on the tripwire branch. If a guardrail raises a non-tripwire exception, the unfinished sibling tasks are left scheduled and continue running past the function's return — wasting work, possibly producing observable side effects, and triggering "Task was destroyed but it is pending" warnings at loop shutdown. Wrap the as_completed loop in try / except BaseException, and on any exit that is not a normal return cancel still-running siblings and gather them (return_exceptions=True) before re-raising. This is distinct from PR openai#3187, which targets the tripwire path of run_output_guardrails. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
5f2fe20 to
980c2b7
Compare
|
Rebased onto current main. The branch now applies cleanly with no conflicts. I re-ran the suite locally against main: ruff format, ruff lint, and mypy all pass, and the 40 tests in tests/test_guardrails.py are green. The change stays narrow, it cancels the sibling guardrail tasks when one raises so they do not leak, and keeps the existing tripwire await path intact. Ready for another look when you have a moment. |
|
Checked again today: the branch still merges cleanly against main (no conflicts), so the resolve-conflicts request is satisfied. The change is scoped to run_input_guardrails and run_output_guardrails in src/agents/run_internal/guardrails.py: the as_completed loop is wrapped so that on any error path, a guardrail raising a non-tripwire exception or the caller being cancelled, the sibling tasks are cancelled and awaited before the exception propagates, which stops them leaking past the function return. The existing tripwire path is unchanged. Tests in tests/test_guardrails.py cover the new behavior and pass locally with ruff and mypy clean. This is ready for another look whenever you have a moment, and I am happy to rebase again if anything has drifted. |
Summary
run_input_guardrailsandrun_output_guardrailsinsrc/agents/run_internal/guardrails.pyiterate guardrail tasks viaasyncio.as_completedand only cancel siblings on the tripwire branch. If one guardrail raises a non-tripwire exception, the loop exits but unfinished sibling tasks are left scheduled and continue running past the function's return — wasting work, possibly producing observable side effects (e.g. an LLM-based guardrail finishing a network request after the caller already saw a different exception), and triggeringTask was destroyed but it is pending!warnings at event-loop shutdown.The fix wraps the
as_completedloop intry / except BaseExceptionand, on any exit that is not a normal return, cancels still-running siblings andgathers them (return_exceptions=True) before re-raising. This is distinct from #3187, which targets the tripwire branch ofrun_output_guardrails; this PR targets the raise branch in both functions.Repro
Test plan
test_input_guardrail_raise_cancels_siblings— asserts the slow sibling is cancelled (not completed) by the time the raise propagates.test_output_guardrail_raise_cancels_siblings— same for output guardrails.tests/test_guardrails.py).tests/test_runner_guardrail_resume.py,tests/test_stream_input_guardrail_timing.py,tests/test_tool_guardrails.pystill pass.ruff checkpasses for changed files.🤖 Generated with Claude Code