Repository navigation
Parent slow-async-callback spans to the running task - #2209
Conversation
`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
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015XEAkoN1Fmcf5WaCGJ2i1P
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🔗 Linked repositories identifiedCodeRabbit considers these linked repositories for cross-repo context during reviews:
📝 WalkthroughWalkthroughSlow asyncio callback warnings now execute in the callback’s captured context. The test wraps callback execution in an 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
logfire/_internal/async_.pytests/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)
There was a problem hiding this comment.
All reported issues were addressed across 1 file (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Problem
log_slow_async_callbacks()patchesasyncio.events.Handle._runand emits itsAsync {name} blocked for {duration:.3f} secondswarning after the wrappedoriginal_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.warnopens 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
test_slow_async_callbacksis unchanged and still passes (its example blocks with no span active, so those spans correctly remain roots).test_slow_async_callback_parented_to_active_span: blocks the loop inside an activelogfire.spanand asserts the resultingAsync ... blockedspan parents to that span, in the same trace. Before this fix every such span was a root.