File tree Expand file tree Collapse file tree 1 file changed +10
-0
lines changed Expand file tree Collapse file tree 1 file changed +10
-0
lines changed Original file line number Diff line number Diff 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" ,
You can’t perform that action at this time.
0 commit comments