fix(run): cancel the parallel input-guardrail task when the model turn fails - #3982
Merged
seratch merged 1 commit intoJul 27, 2026
Merged
Conversation
…n fails In the parallel input-guardrail path, the guardrail coroutine and the model turn are awaited together with asyncio.gather without return_exceptions=True, and the only handler catches InputGuardrailTripwireTriggered. When the model turn (or a guardrail) raises any other exception, gather propagates it but does not cancel the sibling awaitable, so it is orphaned and keeps running network calls and side effects after the run has already failed and returned. An exception raised inside the guardrail task after gather resolves is also swallowed. Wrap the guardrail coroutine in an explicit task and add a symmetric cleanup branch: on any non-tripwire failure, cancel whichever of the guardrail/model tasks is still pending and drain both with return_exceptions=True before re-raising. The existing tripwire behavior and the flag-gated model-task cancellation are preserved.
seratch
approved these changes
Jul 27, 2026
Contributor
Author
|
Thanks for the review and merge, @seratch. Good to have the parallel path cancel and drain the guardrail task instead of leaving it running after the turn fails. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In the parallel input-guardrail path (
AgentRunner.run), the guardrail coroutine and the model turn are awaited together withasyncio.gatherwithoutreturn_exceptions=True, and the only handler catchesInputGuardrailTripwireTriggered.If the model turn raises any other exception (a
ModelBehaviorError, a providerRuntimeError) before the guardrail finishes,gatherpropagates the model error but leaves the sibling guardrail task running. It keeps making network calls and side effects after the run has already failed and returned, and an error raised inside that guardrail aftergatherresolves is silently swallowed. Guardrails default torun_in_parallel=True, so this is the common path whenever an input guardrail exists.The fix wraps the guardrail coroutine in an explicit task and adds a symmetric cleanup branch: on any non-tripwire failure, cancel whichever of the guardrail and model tasks is still pending and drain both with
return_exceptions=Truebefore re-raising. The existing tripwire behavior and the flag-gated model-task cancellation are preserved, and the streamed path is unchanged.This complements #3239, which cancelled sibling guardrails inside
run_input_guardrailsitself. That fix does not reach the orchestration-levelgatherinrun.py, so when the model task is the one that raises, the guardrail task is still orphaned. This closes that gap.Tests cover a model error cancelling the in-flight guardrail task, and a guardrail's non-tripwire error propagating and cancelling the model task; the existing tripwire tests still pass. Both new tests fail on
mainand pass with this change. Full suite: 5783 passed, 7 skipped; ruff, format, and mypy clean.