Skip to content

Conversation

@jy-tan
Copy link
Contributor

@jy-tan jy-tan commented Jan 17, 2026

Summary

BatchSpanProcessor previously called asyncio.new_event_loop() for every batch export when using async adapters. Creating and destroying event loops is expensive (involves allocating selectors, registering file descriptors, and other OS-level setup/teardown) and can show up as latency/jitter under load. This change creates a single long-lived event loop when the export thread starts and reuses it for all async exports.

Changes

  • Add _thread_loop instance variable to store the long-lived event loop
  • Create the event loop once at the start of _export_loop() instead of per-export
  • Reuse the thread's event loop in _export_batch() for async adapter exports
  • Properly clean up the loop in a finally block when the export thread exits
  • Retain fallback to temporary loop creation for _force_flush() after shutdown (rare, one-time occurrence when the thread has already exited)
  • Unit tests: 5 new tests in tests/unit/test_batch_processor.py

Copy link

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

Choose a reason for hiding this comment

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

1 issue found across 1 file

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="drift/core/batch_processor.py">

<violation number="1" location="drift/core/batch_processor.py:203">
P2: Reusing the export thread’s event loop without verifying the current thread can break force_flush when stop() times out. In that case, _force_flush() runs on a different thread and will attempt to drive the export thread’s loop, causing a RuntimeError. Gate the reuse on running in the export thread (or always create a temporary loop off-thread).</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@jy-tan jy-tan merged commit faf8a50 into main Jan 18, 2026
17 checks passed
@jy-tan jy-tan deleted the event-loop branch January 18, 2026 00:38
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