{"record":{"id":"d730b95f37bfdf6d","repo":"microsoft/autogen","slug":"runtime-is-not-running-d730b9","errorCode":null,"errorMessage":"Runtime is not running.","messagePattern":"Runtime is not running\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py","lineNumber":311,"sourceCode":"                    case \"response\":\n                        task = asyncio.create_task(self._process_response(message.response))\n                        self._background_tasks.add(task)\n                        task.add_done_callback(self._raise_on_exception)\n                        task.add_done_callback(self._background_tasks.discard)\n                    case \"cloudEvent\":\n                        task = asyncio.create_task(self._process_event(message.cloudEvent))\n                        self._background_tasks.add(task)\n                        task.add_done_callback(self._raise_on_exception)\n                        task.add_done_callback(self._background_tasks.discard)\n                    case None:\n                        logger.warning(\"No message\")\n            except Exception as e:\n                logger.error(\"Error in read loop\", exc_info=e)\n\n    async def stop(self) -> None:\n        \"\"\"Stop the runtime immediately.\"\"\"\n        if not self._running:\n            raise RuntimeError(\"Runtime is not running.\")\n        self._running = False\n        # Wait for all background tasks to finish.\n        final_tasks_results = await asyncio.gather(*self._background_tasks, return_exceptions=True)\n        for task_result in final_tasks_results:\n            if isinstance(task_result, Exception):\n                logger.error(\"Error in background task\", exc_info=task_result)\n        # Close the host connection.\n        if self._host_connection is not None:\n            try:\n                await self._host_connection.close()\n            except asyncio.CancelledError:\n                pass\n        # Cancel the read task.\n        if self._read_task is not None:\n            self._read_task.cancel()\n            try:\n                await self._read_task\n            except asyncio.CancelledError:","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py#L293-L329","documentation":"GrpcWorkerAgentRuntime.stop() refuses to stop a runtime that is not running: if _running is False it raises RuntimeError('Runtime is not running.'). Stop is only valid after a successful start(), making the lifecycle strict and errors loud.","triggerScenarios":"Calling `await runtime.stop()` before start(), after stop() already completed, or in a shutdown/cleanup path where start() was skipped because setup failed early.","commonSituations":"finally blocks and signal handlers that unconditionally stop the runtime; error paths where start() raised before setting _running; double-stop in atexit/asyncio shutdown hooks.","solutions":["Mirror the runtime lifecycle: only stop what you started, tracking a started flag","In cleanup code, tolerate the error: try/except RuntimeError around stop()","Prefer stopping inside the same scope that called start() (async context manager pattern)"],"exampleFix":"# before\nfinally:\n    await runtime.stop()  # RuntimeError if never started\n\n# after\nfinally:\n    if runtime._running:\n        await runtime.stop()\n    # or: wrap in try/except RuntimeError and ignore","handlingStrategy":"try-catch","validationCode":"if runtime._running:  # private but reliable mirror of the check\n    await runtime.stop()","typeGuard":null,"tryCatchPattern":"try:\n    await runtime.stop()\nexcept RuntimeError as e:\n    if \"not running\" in str(e):\n        pass  # nothing to stop\n    else:\n        raise","preventionTips":["Keep start/stop in the same scope (context manager pattern)","Set a started flag and check it before stop()","In shutdown hooks, tolerate stop() failures"],"tags":["python","autogen","grpc","lifecycle","shutdown"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}