{"record":{"id":"8fde69f52ca7734e","repo":"microsoft/semantic-kernel","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/semantic_kernel/agents/runtime/in_process/in_process_runtime.py","lineNumber":655,"sourceCode":"                                    sender=sender,\n                                    receiver=recipient,\n                                    kind=MessageKind.RESPOND,\n                                )\n                            )\n                            future.set_exception(MessageDroppedException())\n                            return\n                        message_envelope.message = temp_message\n                task = asyncio.create_task(self._process_response(message_envelope))\n                self._background_tasks.add(task)\n                task.add_done_callback(self._background_tasks.discard)\n\n        # Yield control to the message loop to allow other tasks to run\n        await asyncio.sleep(0)\n\n    def start(self) -> None:\n        \"\"\"Start the runtime message processing loop. This runs in a background task.\"\"\"\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.\n\n        The currently processing message will be completed, but all others following it will be discarded.\n        \"\"\"\n        if self._run_context is None:","sourceCodeStart":637,"sourceCodeEnd":673,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/runtime/in_process/in_process_runtime.py#L637-L673","documentation":"InProcessRuntime.start() sets _run_context. If _run_context is already set (runtime was started and not stopped), calling start() again raises RuntimeError. The runtime must be stopped before it can be restarted.","triggerScenarios":"Calling runtime.start() twice without an intervening runtime.stop() (or stop_when_idle/stop_when). Common in test teardown/re-setup cycles or when reusing a runtime instance across runs.","commonSituations":"A test fixture starts the runtime in setUp but forgets to stop it in tearDown, then a second test calls start() on the same instance. Or application code that restarts the runtime on reconnect/retry without stopping first.","solutions":["Call await runtime.stop() (or stop_when_idle) before calling start() again.","Use runtime.close() for full teardown, which internally calls stop().","Create a fresh InProcessRuntime instance for each independent run rather than reusing one."],"exampleFix":"# before\nruntime.start()\n# ... use runtime ...\nruntime.start()  # raises 'Runtime is already started'\n\n# after\nruntime.start()\n# ... use runtime ...\nawait runtime.stop_when_idle()\nruntime.start()  # ok","handlingStrategy":"validation","validationCode":"# Check runtime state before starting\nif runtime._run_context is not None:\n    raise RuntimeError('Runtime already started — stop it first')\nruntime.start()","typeGuard":"null","tryCatchPattern":"try:\n    runtime.start()\nexcept RuntimeError:\n    await runtime.stop()\n    runtime.start()","preventionTips":["Always pair start() with stop()/stop_when_idle()/close().","Create a fresh InProcessRuntime for each independent session rather than restarting.","Use try/finally to ensure stop is called after start succeeds."],"tags":["agent-runtime","lifecycle","start","in-process-runtime","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}