Skip to content

fix: preserve exceptions in instrumented async context managers - #2767

Merged
alexmojaki merged 1 commit into
pydantic:mainfrom
peterolkhov:codex/preserve-instrumented-async-context-exceptions
Nov 10, 2026
Merged

alexmojaki merged 1 commit into
pydantic:mainfrom
peterolkhov:codex/preserve-instrumented-async-context-exceptions

Conversation

@peterolkhov

@peterolkhov peterolkhov commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

With @asynccontextmanager above @logfire.instrument(allow_generator=True), exceptions in the async with body stop at the instrumentation wrapper. The original manager never receives them, so rollback and exception suppression can be skipped and cleanup runs late. A local SQLite reproduction leaves a failed transaction open; the next successful commit also commits its write.

Forward exceptions to the original async generator while keeping the span open during cleanup. Regression tests cover propagation, suppression, exception identity, replacement errors, cancellation, close, and ordinary iteration. The sending-values limitation remains unchanged.

Tested on the checkout: test_logfire.py and test_metrics.py pass on Python 3.13 (162 passed, 1 skipped) and 3.10 (161 passed, 2 skipped); Ruff formatting/lint and changed-file Pyright pass. The public SQLite reproduction now rolls back before exit. Focused instrument.py line/branch coverage is 100%; combined matrix coverage still needs CI.

Full-project local Pyright lacks optional integration dependencies and reports exactly the same output on pristine base and this patch. The full integration suite wasn't run locally.

Implemented with GPT-6.1 Sol (high); the coordinator reviewed the diff and independently reran the SQLite reproduction before and after.

View guided diff

@veria-ai

veria-ai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

View guided diff | Re-trigger cubic

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-sdk/logfire/_internal
  instrument.py
Project Total  

This report was generated by python-coverage-comment-action

@alexmojaki
alexmojaki added this pull request to the merge queue Nov 10, 2026
Merged via the queue into pydantic:main with commit 272e412 Nov 10, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants