Skip to content

Commit db58955

Browse files
committed
fix: cancel remaining guardrails when blocking guardrail triggers
1 parent 339895f commit db58955

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/agents/run.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,11 @@ async def _run_input_guardrails_with_queue(
964964
for done in asyncio.as_completed(guardrail_tasks):
965965
result = await done
966966
if result.output.tripwire_triggered:
967+
# Cancel all remaining guardrail tasks if a tripwire is triggered.
968+
for t in guardrail_tasks:
969+
t.cancel()
970+
# Wait for cancellations to propagate by awaiting the cancelled tasks.
971+
await asyncio.gather(*guardrail_tasks, return_exceptions=True)
967972
_error_tracing.attach_error_to_span(
968973
parent_span,
969974
SpanError(
@@ -974,6 +979,9 @@ async def _run_input_guardrails_with_queue(
974979
},
975980
),
976981
)
982+
queue.put_nowait(result)
983+
guardrail_results.append(result)
984+
break
977985
queue.put_nowait(result)
978986
guardrail_results.append(result)
979987
except Exception:
@@ -1669,6 +1677,8 @@ async def _run_input_guardrails(
16691677
# Cancel all guardrail tasks if a tripwire is triggered.
16701678
for t in guardrail_tasks:
16711679
t.cancel()
1680+
# Wait for cancellations to propagate by awaiting the cancelled tasks.
1681+
await asyncio.gather(*guardrail_tasks, return_exceptions=True)
16721682
_error_tracing.attach_error_to_current_span(
16731683
SpanError(
16741684
message="Guardrail tripwire triggered",

0 commit comments

Comments
 (0)