fix(tracing): never let span export block the event loop or the run path - #199
Merged
Conversation
Two fixes for host-visible latency caused by span export: 1. Tracer.force_flush / Meter.force_flush wrapped the provider's synchronous force_flush directly in an async def — awaiting it parked the sync flush ON the event loop thread, freezing every concurrent task in the host process until the exporters drained (seconds against a remote/backlogged OTLP collector). Both now run the provider flush via asyncio.to_thread. 2. trace() gains flush="await"|"background". The default keeps today's exit-means-exported guarantee. "background" detaches synchronously but supervises the export as a tracked background task — for serving paths where a caller is waiting on the block (measured 8.6s added to chat-turn completion in cubebox before this). The tracer holds strong refs to pending flushes and shutdown() settles them, so spans still export on clean shutdown; supervisor logs failures per this helper's swallow-and-log contract.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
codecov/patch flagged the meter's to_thread flush lines — no existing meter test exercised force_flush. Mirror the tracer's off-loop thread assertion.
xfgong
added a commit
to cubeplexai/cubeplex
that referenced
this pull request
Jul 6, 2026
cubeplexai/cubepi#199 merged; move the pin from the branch commit to the mainline SHA (same code, rebase-merged).
xfgong
added a commit
to cubeplexai/cubeplex
that referenced
this pull request
Jul 6, 2026
cubeplexai/cubepi#199 merged; move the pin from the branch commit to the mainline SHA (same code, rebase-merged).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Diagnosed in cubebox: every chat turn's `done` event lagged the final `usage`
event by 5–20s. Timing probes put 8.65s inside the exit of
`async with trace(tracer, agent)` — span export was gating a user-visible
response, and worse, freezing the whole event loop while it ran:
the provider's synchronous `force_flush` inline. Awaiting them parked a
blocking flush on the loop thread: every concurrent request in the host
process stalls until the exporters drain. Against a remote/backlogged OTLP
collector that's seconds per run.
Right default for scripts; wrong for serving paths where a caller is
blocked on the `async with` body's completion.
Fix
`asyncio.to_thread` — awaiting them never stalls the loop.
line), export runs as a supervised background task. The tracer holds
strong refs (loop refs are weak) and `shutdown()` settles pending
flushes, so spans still export on clean shutdown. Supervisor logs
failures, matching `trace()`'s swallow-and-log contract.
Tests
`tests/tracing/test_nonblocking_flush.py`:
concurrently ticking task
`shutdown()` settles it; failures are logged
Full suite: 1842 passed, 1 skipped. ruff + format + mypy clean.
🤖 Generated with Claude Code