Skip to content

Parent slow-async-callback spans to the running task - #2209

Merged
alexmojaki merged 5 commits into
mainfrom
claude/async-span-parent-patch-y9hicq
Aug 6, 2026
Merged

alexmojaki merged 5 commits into
mainfrom
claude/async-span-parent-patch-y9hicq

Conversation

@jirikuncar

@jirikuncar jirikuncar commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem

log_slow_async_callbacks() patches asyncio.events.Handle._run and emits its Async {name} blocked for {duration:.3f} seconds warning after the wrapped original_run(self) returns. At that point execution is back in the event loop's top-level context, where no span is active, so the warning span parented to nothing and landed in a trace of its own, detached from the task step that was actually blocking the loop.

Fix

Emit the warning via self._context.run(logfire.warn, ...), i.e. inside the context the callback just ran in, where the task's span is still active. The warning span now parents to that span in the same trace. logfire.warn opens and closes its span with balanced attach/detach, so running it inside the context leaves the task's context unchanged for its next step.

The change is a single call-site edit; timing and attribute extraction are untouched.

Tests

  • The existing test_slow_async_callbacks is unchanged and still passes (its example blocks with no span active, so those spans correctly remain roots).
  • Added test_slow_async_callback_parented_to_active_span: blocks the loop inside an active logfire.span and asserts the resulting Async ... blocked span parents to that span, in the same trace. Before this fix every such span was a root.

Review in cubic

claude added 2 commits August 6, 2026 11:16
`log_slow_async_callbacks` patches `Handle._run` and emits its
`Async {name} blocked for {duration:.3f} seconds` warning after the wrapped
`original_run(self)` returns, back in the event loop's top-level context where no
span is active. The warning span therefore parented to nothing and landed in a
trace of its own, detached from the task step that was actually blocking the loop.

Emit the warning inside `self._context.run(...)`, the context the callback just
ran in, where the task's span is still active, so the warning span parents to it.
`logfire.warn` balances its own attach/detach, leaving that context unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015XEAkoN1Fmcf5WaCGJ2i1P
@coderabbitai

coderabbitai Bot commented Aug 6, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7ade6e80-f66a-4cb3-a31a-88eb905f5aac

📥 Commits

Reviewing files that changed from the base of the PR and between 7e6242d and c047e72.

📒 Files selected for processing (2)
  • tests/import_used_for_tests/slow_async_callbacks_example.py
  • tests/test_slow_async_callbacks.py
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • pydantic/logfire (manual)
  • pydantic/pydantic-ai (manual)
  • pydantic/platform (auto-detected)
  • pydantic/pydantic (auto-detected)

📝 Walkthrough

Walkthrough

Slow asyncio callback warnings now execute in the callback’s captured context. The test wraps callback execution in an in bar span and verifies deterministic trace and span identifiers for each callback span. The assertions verify that callback spans use the in bar span as their parent. The snapshot includes the exported in bar span with its context, source metadata, and attributes.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: parenting slow asynchronous callback warnings to the running task.
Description check ✅ Passed The description clearly explains the detached warning span problem, the context-based fix, and the tests that verify correct span parenting.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/async-span-parent-patch-y9hicq

Comment @coderabbitai help to get the list of available commands.

@jirikuncar jirikuncar self-assigned this Aug 6, 2026
@jirikuncar
jirikuncar requested a review from alexmojaki August 6, 2026 11:30

@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.

Re-trigger cubic

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_slow_async_callbacks.py`:
- Line 32: Update the exported span assertion in the affected test to pass
parse_json_attributes=True explicitly, preserving the existing TestExporter and
inline snapshot structure. Refresh the inline snapshot if enabling parsed
attributes changes its representation.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d50a80f1-826e-4879-9ab1-efabe4496bbd

📥 Commits

Reviewing files that changed from the base of the PR and between 5f1fb5f and 7e6242d.

📒 Files selected for processing (2)
  • logfire/_internal/async_.py
  • tests/test_slow_async_callbacks.py
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • pydantic/logfire (manual)
  • pydantic/pydantic-ai (manual)
  • pydantic/platform (auto-detected)
  • pydantic/pydantic (auto-detected)

Comment thread tests/test_slow_async_callbacks.py Resolved

@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.

All reported issues were addressed across 1 file (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread tests/test_slow_async_callbacks.py Resolved
@alexmojaki
alexmojaki enabled auto-merge August 6, 2026 12:21
@alexmojaki
alexmojaki added this pull request to the merge queue Aug 6, 2026
Merged via the queue into main with commit 08a8336 Aug 6, 2026
18 checks passed
@alexmojaki
alexmojaki deleted the claude/async-span-parent-patch-y9hicq branch August 6, 2026 12:34
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.

3 participants