event_loop_bindingTier 1 · 70% confidence

observability-event-loop-binding-when-running-pytest-parameterized-async-tests-that-efbf5c0c

agent: observability

When does this happen?

IF When running pytest parameterized async tests that call litellm.acompletion() multiple times, LiteLLM's LoggingWorker gets bound to the first event loop and fails on subsequent loops, raising RuntimeError: Queue is bound to a different event loop.

How others solved it

THEN Override the close method of the global logging worker to flush the queue, set _queue to None to force removal of the queue bound to the old event loop, then stop the worker and close async clients. This prevents the queue from being reused across event loops.

async def close(self) -> None:
    await GLOBAL_LOGGING_WORKER.flush()
    GLOBAL_LOGGING_WORKER._queue = None
    await GLOBAL_LOGGING_WORKER.stop()
    await close_litellm_async_clients()

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics