{"record":{"id":"54340d2d3c7e0dd2","repo":"microsoft/autogen","slug":"runtime-is-already-started","errorCode":null,"errorMessage":"Runtime is already started","messagePattern":"Runtime is already started","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py","lineNumber":820,"sourceCode":"\n            import asyncio\n            from autogen_core import SingleThreadedAgentRuntime\n\n\n            async def main() -> None:\n                runtime = SingleThreadedAgentRuntime()\n                runtime.start()\n\n                # ... do other things ...\n\n                await runtime.stop()\n\n\n            asyncio.run(main())\n\n        \"\"\"\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:","sourceCodeStart":802,"sourceCodeEnd":838,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py#L802-L838","documentation":"SingleThreadedAgentRuntime.start() raises RuntimeError if self._run_context is already set, i.e. the runtime's message loop is already running. The runtime supports only one active run context at a time; start() is not idempotent.","triggerScenarios":"Calling runtime.start() twice without an intervening stop(); calling start() in a retry wrapper that re-runs startup; starting the same runtime object again after an exception path that did not reset _run_context.","commonSituations":"Long-running apps that reconnect/restart on failure and call start() again; test fixtures that start a shared runtime per test; notebooks where a cell invoking start() is executed twice.","solutions":["Guard with the runtime's own state: only call start() when needed, and always pair start() with stop()/stop_when_idle() (stop() resets _run_context)","Use stop_when_idle() + start() cycle per phase instead of multiple starts","Create a fresh SingleThreadedAgentRuntime instance for each run session rather than restarting the same one"],"exampleFix":"# before\nruntime.start()\nruntime.start()  # RuntimeError\n\n# after\nruntime.start()\n...\nawait runtime.stop_when_idle()\nruntime.start()  # now valid","handlingStrategy":"validation","validationCode":"def start_once(runtime):\n    if runtime._run_context is None:\n        runtime.start()\n        return True\n    return False","typeGuard":null,"tryCatchPattern":"try:\n    runtime.start()\nexcept RuntimeError as e:\n    if \"already started\" in str(e):\n        pass  # treat as success\n    else:\n        raise","preventionTips":["Encapsulate runtime lifecycle in one orchestrator function","Pair every start() with a stop-family call in a finally block","Create a new runtime instance per session instead of restarting"],"tags":["autogen-core","runtime","lifecycle","duplicate-start"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}