{"record":{"id":"0b25501f71ede8a7","repo":"nicolargo/glances","slug":"asyncio-event-loop-is-none-after-initialization","errorCode":null,"errorMessage":"AsyncIO event loop is None after initialization","messagePattern":"AsyncIO event loop is None after initialization","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"glances/exports/export_asyncio.py","lineNumber":57,"sourceCode":"        # 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:\n            self.loop = asyncio.new_event_loop()\n            asyncio.set_event_loop(self.loop)\n            self._loop_ready.set()\n            self.loop.run_forever()\n        except Exception as e:\n            self._loop_exception = e","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/nicolargo/glances/blob/a240d8dfb3105a38b5964357ec21768594b0e83e/glances/exports/export_asyncio.py#L39-L75","documentation":"Defensive check after the ready-wait and exception checks: if self.loop is still None the thread reported neither readiness failure nor success, leaving the object unusable. In practice this is a near-unreachable invariant guard indicating a logic bug or race in export_asyncio.py rather than a user misconfiguration.","triggerScenarios":"Race conditions where _loop_ready is set but self.loop assignment has not been observed yet (missing memory synchronization), or third-party subclasses interfering with the loop setup lifecycle.","commonSituations":"Custom export subclasses that override _run_event_loop or manipulate self.loop; extremely rare in stock usage.","solutions":["If subclassing, ensure you call super()._run_event_loop() and don't assign self.loop = None yourself.","Update Glances — this guard has been hardened across versions.","Report a bug with a reproducer if it occurs with stock exporters."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    export = MyAsyncExport(...)\nexcept RuntimeError as e:\n    if 'loop is None' in str(e):\n        # internal invariant: report upstream, fall back to sync export","preventionTips":["Don't subclass async exporters overriding _run_event_loop or self.loop.","Keep Glances updated — this guard has been hardened over releases.","Treat occurrences as a bug; capture the export name and stack trace for a report."],"tags":["asyncio","invariant","export","defensive-check"],"backgroundTag":"asyncio-event-loop-startup-failure","analyzedSha":"a240d8dfb3105a38b5964357ec21768594b0e83e","analyzedAt":"2026-08-27T19:15:19.178Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}