{"record":{"id":"07b8fe110ae0caf5","repo":"nicolargo/glances","slug":"asyncio-event-loop-failed-to-start-within-timeout","errorCode":null,"errorMessage":"AsyncIO event loop failed to start within timeout","messagePattern":"AsyncIO event loop failed to start within timeout","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"glances/exports/export_asyncio.py","lineNumber":51,"sourceCode":"    \"\"\"\n\n    def __init__(self, config=None, args=None):\n        \"\"\"Init the AsyncIO export interface.\"\"\"\n        super().__init__(config=config, args=args)\n\n        # AsyncIO event loop management\n        self.loop = None\n        self._loop_ready = threading.Event()\n        self._loop_exception = None\n        self._shutdown = False\n\n        # Start the background event loop thread\n        self._loop_thread = threading.Thread(target=self._run_event_loop, daemon=True)\n        self._loop_thread.start()\n\n        # Wait for the loop to be ready\n        if not self._loop_ready.wait(timeout=10):\n            raise RuntimeError(\"AsyncIO event loop failed to start within timeout\")\n\n        if self._loop_exception:\n            raise RuntimeError(f\"AsyncIO event loop creation failed: {self._loop_exception}\")\n\n        if self.loop is None:\n            raise RuntimeError(\"AsyncIO event loop is None after initialization\")\n\n        # Call child class AsyncIO initialization\n        future = asyncio.run_coroutine_threadsafe(self._async_init(), self.loop)\n        try:\n            future.result(timeout=10)\n            logger.debug(f\"{self.export_name} AsyncIO export initialized successfully\")\n        except Exception as e:\n            logger.warning(f\"{self.export_name} AsyncIO initialization failed: {e}. Will retry in background.\")\n\n    def _run_event_loop(self):\n        \"\"\"Run event loop in background thread.\"\"\"\n        try:","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/nicolargo/glances/blob/a240d8dfb3105a38b5964357ec21768594b0e83e/glances/exports/export_asyncio.py#L33-L69","documentation":"The AsyncIO export helper starts a background thread that creates an asyncio event loop and signals readiness via an Event. This RuntimeError is raised in __init__ when the loop thread does not set _loop_ready within 10 seconds, meaning the background loop never became usable. Typical causes are a dead/blocked loop thread, an exception inside the thread before signaling, or severe thread/CPU starvation at startup.","triggerScenarios":"Instantiating an export class deriving from GlancesAsyncExport (e.g. export_mqtt or similar async exporters) on a heavily loaded system, in an interpreter where the loop thread crashed, or where _run_event_loop exited before calling _loop_ready.set(). Also seen in restricted environments (some containers/sandboxes) that block thread creation.","commonSituations":"Embedded runs, Docker containers with low CPU quota, exotic interpreters (PyPy/Windows forks), or when the daemon thread is killed as the process shuts down during export init.","solutions":["Check stderr/logs for the companion error 'AsyncIO event loop creation failed' — the thread usually records why it never signaled ready.","Retry initialization once; transient thread scheduling delays can exceed the 10s timeout on loaded machines.","Increase available CPU/threads for the process (raise container CPU limits, avoid mass-export plugin loading at startup).","Inspect export_asyncio.py's _run_event_loop to see where _loop_ready.set() happens and whether the loop creation raised; report a bug if the exception path bypasses signalling."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"from glances.exports.export_asyncio import GlancesAsyncExport\nimport inspect\nsrc = inspect.getsource(GlancesAsyncExport)\nassert '_loop_ready' in src  # sanity: helper API present","typeGuard":null,"tryCatchPattern":"try:\n    export = MyAsyncExport(...)\nexcept RuntimeError as e:\n    if 'event loop failed to start' in str(e):\n        export = None  # degrade gracefully, retry once","preventionTips":["Avoid instantiating many async exporters simultaneously at startup; stagger or lazy-init them.","Ensure the runtime environment permits thread creation and isn't CPU-starved at init.","Monitor for the companion 'event loop creation failed' message which carries the root cause."],"tags":["asyncio","threading","startup-timeout","export"],"backgroundTag":"asyncio-event-loop-startup-failure","analyzedSha":"a240d8dfb3105a38b5964357ec21768594b0e83e","analyzedAt":"2026-08-27T19:15:19.178Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}