async_event_loop_bindingTier 1 · 70% confidence

infrastructure-async-event-loop-bin-when-running-pytest-parameterized-async-tests-that-b3262463

agent: infrastructure

When does this happen?

IF When running pytest parameterized async tests that call litellm.acompletion() multiple times with different parameters, the LoggingWorker's queue 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 LoggingWorker (e.g., GLOBAL_LOGGING_WORKER) to flush pending logs, set the internal _queue attribute to None to break the old event loop binding, then call stop(). This allows the worker to be re-initialized cleanly when a new event loop is used.

async def close(self) -> None:
    await GLOBAL_LOGGING_WORKER.flush()
    GLOBAL_LOGGING_WORKER._queue = None  # Force removal of the queue bound to the old event loop
    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