Skip to content

fix: handle BaseException in trace spans to prevent span leaks on Key…#2068

Open
Di-Is wants to merge 1 commit intostrands-agents:mainfrom
Di-Is:fix/base-exception-span-handling
Open

fix: handle BaseException in trace spans to prevent span leaks on Key…#2068
Di-Is wants to merge 1 commit intostrands-agents:mainfrom
Di-Is:fix/base-exception-span-handling

Conversation

@Di-Is
Copy link
Copy Markdown
Contributor

@Di-Is Di-Is commented Apr 5, 2026

Description

PR #2032 switched spans from end_on_exit=True to explicit span.end() calls, fixing span timing. However, the existing except Exception handlers don't catch BaseException subclasses like KeyboardInterrupt and asyncio.CancelledError, so spans leak when these occur.

This PR ensures spans are properly closed on all exit paths, including BaseException interruptions and premature async generator teardown.

Follow-up to #2032 (which resolved #1930), split from #1939 per reviewer request.

Public API Changes

No public API changes. Internal tracer method signatures are widened from Exception to BaseException (backward-compatible).

Related Issues

Documentation PR

N/A

Type of Change

Bug fix

Testing

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…boardInterrupt

Trace spans were not properly closed when BaseException (e.g. KeyboardInterrupt,
asyncio.CancelledError) was raised. Add explicit BaseException handlers to close
spans and aclose() calls to ensure async generators are cleaned up.
@github-actions github-actions bot added the size/m label Apr 5, 2026
@Di-Is Di-Is requested a deployment to manual-approval April 5, 2026 12:59 — with GitHub Actions Waiting
@Di-Is Di-Is requested a deployment to manual-approval April 5, 2026 12:59 — with GitHub Actions Waiting
@Di-Is
Copy link
Copy Markdown
Contributor Author

Di-Is commented Apr 5, 2026

@zastrowm Spun out the BaseException handling commit as a standalone PR (#2032). :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Regression: event_loop_cycle span duration becomes cumulative across recursive cycles

1 participant