← Back to Pull requests

chore(internal): reset the execution context when a context.ended listener raises #22342

Open
unknown opened this pull request with 334 lines diff across 4 files
View on GitHub
unknown unknown commented on Dec 03, 2026 08:05 UTC
Author

Description

Part of APPSEC-71906. Split out of #22145 on review: the LiteLLM change does not need it.

A context.ended listener that raised a BaseException (for example a DDBlockException, which core.dispatch always propagates) skipped the rest of ExecutionContext.__exit__:

  • the current context was not reset, so core.current kept pointing at the ended context;

  • the context was not marked ended, so a later dispatch_ended_event dispatched the ended event again.
  • __exit__ and dispatch_ended_event now mark the context ended and reset the current context in finally blocks, 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.ended listener raises a BaseException. 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 from context.ended leaves core.current back 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.current is back at the still-open parent before the parent exits;

  • test_core_dispatch_ended_event_base_exception_marks_dispatched: after a raising listener, a second dispatch_ended_event is a no-op.
  • All three fail without the change. tests/internal/test_context_events_api.py passes locally (py3.12).

    Risks

    Low. ExecutionContext.__exit__ runs for every context, so scripts/run-benchmarks was 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/finally blocks only do work when a listener raises.

    🤖 Generated with Claude Code

    [APPSEC-71906]: https://datadoghq.atlassian.net/browse/APPSEC-71906?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 334 lines (+250 / -84) across 4 files Reviewer: dubloom Review duration: 1497m
    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
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.2% (MEDIUM) · Higher = larger blast radius
    47.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.1% · Safe to merge
    25.1%