chore(internal): reset the execution context when a context.ended listener raises #22342
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.47) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 334 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.348
Shallow Review Depth
shallow_review_depth
0.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #22342"
general
weight: 0.35
dubloom
Description
Part of APPSEC-71906. Split out of #22145 on review: the LiteLLM change does not need it.
A
context.endedlistener that raised aBaseException(for example aDDBlockException, whichcore.dispatchalways propagates) skipped the rest ofExecutionContext.__exit__:core.currentkept pointing at the ended context;dispatch_ended_eventdispatched the ended event again.__exit__anddispatch_ended_eventnow mark the context ended and reset the current context infinallyblocks, so both happen even when a listener raises. The listener's exception still propagates.With a nested context, the contextvar was reset only when the parent exited, so code still running in the parent saw the ended child as
core.current. After a top-level context, it was never reset.Customer impact
None on its own: today no
context.endedlistener raises aBaseException. AI Guard blocks from one once #22039 lands. No release note.Testing
tests/internal/test_context_events_api.py:test_core_context_ended_base_exception_resets_context: a listener that raises fromcontext.endedleavescore.currentback at the root context, and the context is marked ended;test_core_context_ended_base_exception_restores_parent_context: when a nested context's listener raises,core.currentis back at the still-open parent before the parent exits;test_core_dispatch_ended_event_base_exception_marks_dispatched: after a raising listener, a seconddispatch_ended_eventis a no-op.All three fail without the change.
tests/internal/test_context_events_api.pypasses locally (py3.12).Risks
Low.
ExecutionContext.__exit__runs for every context, soscripts/run-benchmarkswas run against the commit before the change (2bef77f093), Python 3.12.10, two runs of the context configs:context_with_data_no_listeners: +3.7% / +0.65% (not significant);context_with_data_listeners(10 listeners): +14.8% / +5.0% (~0.2 µs);get_item_missing(unchanged code, control): +16.9% / −2.3%;events_api(context with subscribers): −7.9%.Standard deviations overlap everywhere, so the no-listener path shows no change; contexts with listeners may pay up to ~5%.
Additional Notes
The extra
try/finallyblocks only do work when a listener raises.🤖 Generated with Claude Code
[APPSEC-71906]: https://datadoghq.atlassian.net/browse/APPSEC-71906?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ