{"record":{"id":"ec7ad5e2b7c034ed","repo":"microsoft/autogen","slug":"runtime-is-not-started","errorCode":null,"errorMessage":"Runtime is not started","messagePattern":"Runtime is not started","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py","lineNumber":836,"sourceCode":"        \"\"\"\n        if self._run_context is not None:\n            raise RuntimeError(\"Runtime is already started\")\n        self._run_context = RunContext(self)\n\n    async def close(self) -> None:\n        \"\"\"Calls :meth:`stop` if applicable and the :meth:`Agent.close` method on all instantiated agents\"\"\"\n        # stop the runtime if it hasn't been stopped yet\n        if self._run_context is not None:\n            await self.stop()\n        # close all the agents that have been instantiated\n        for agent_id in self._instantiated_agents:\n            agent = await self._get_agent(agent_id)\n            await agent.close()\n\n    async def stop(self) -> None:\n        \"\"\"Immediately stop the runtime message processing loop. The currently processing message will be completed, but all others following it will be discarded.\"\"\"\n        if self._run_context is None:\n            raise RuntimeError(\"Runtime is not started\")\n\n        try:\n            await self._run_context.stop()\n        finally:\n            self._run_context = None\n            self._message_queue = Queue()\n\n    async def stop_when_idle(self) -> None:\n        \"\"\"Stop the runtime message processing loop when there is\n        no outstanding message being processed or queued. This is the most common way to stop the runtime.\"\"\"\n        if self._run_context is None:\n            raise RuntimeError(\"Runtime is not started\")\n\n        try:\n            await self._run_context.stop_when_idle()\n        finally:\n            self._run_context = None\n            self._message_queue = Queue()","sourceCodeStart":818,"sourceCodeEnd":854,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py#L818-L854","documentation":"stop() raises RuntimeError when self._run_context is None, meaning the runtime's processing loop is not currently running. Stopping requires an active run context; stopping an idle/never-started (or already-stopped) runtime is an error, not a no-op.","triggerScenarios":"Calling await runtime.stop() before any start(), after stop()/stop_when_idle() already completed, or after close() (which itself calls stop when applicable). Double-stop in cleanup paths is the most common form.","commonSituations":"Cleanup/teardown code running on both the success and exception paths so stop() executes twice; orchestrators that stop at end-of-task and again at shutdown; tests tearing down a runtime that a previous line already stopped.","solutions":["Call stop() exactly once per start(); track lifecycle in your orchestrator with a flag or use try/except RuntimeError in cleanup as a deliberate idempotency guard","Prefer await runtime.close() for teardown: it stops only if not already stopped and also closes agents","Ensure close()/stop() are not both invoked in the same teardown sequence for the stop portion"],"exampleFix":"# before\nawait runtime.stop_when_idle()\nawait runtime.stop()  # RuntimeError\n\n# after\nawait runtime.stop_when_idle()\nawait runtime.close()  # safe: no-ops the stop portion, closes agents","handlingStrategy":"try-catch","validationCode":"def can_stop(runtime) -> bool:\n    return runtime._run_context is not None","typeGuard":null,"tryCatchPattern":"try:\n    await runtime.stop()\nexcept RuntimeError as e:\n    if \"not started\" in str(e):\n        pass  # already stopped; idempotent teardown\n    else:\n        raise","preventionTips":["Prefer close() for teardown (it stops only if needed and closes agents)","Run stop in exactly one place (success path or finally, not both unguarded)","Track runtime lifecycle state in your app if you stop at multiple points"],"tags":["autogen-core","runtime","lifecycle","stop"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}