{"record":{"id":"0252dc356ebdfef9","repo":"microsoft/autogen","slug":"host-runtime-is-already-started","errorCode":null,"errorMessage":"Host runtime is already started.","messagePattern":"Host runtime is already started\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime_host.py","lineNumber":36,"sourceCode":"\nclass GrpcWorkerAgentRuntimeHost:\n    def __init__(self, address: str, extra_grpc_config: Optional[ChannelArgumentType] = None) -> None:\n        self._server = grpc.aio.server(options=extra_grpc_config)\n        self._servicer = GrpcWorkerAgentRuntimeHostServicer()\n        agent_worker_pb2_grpc.add_AgentRpcServicer_to_server(self._servicer, self._server)\n        self._server.add_insecure_port(address)\n        self._address = address\n        self._serve_task: asyncio.Task[None] | None = None\n\n    async def _serve(self) -> None:\n        await self._server.start()\n        logger.info(f\"Server started at {self._address}.\")\n        await self._server.wait_for_termination()\n\n    def start(self) -> None:\n        \"\"\"Start the server in a background task.\"\"\"\n        if self._serve_task is not None:\n            raise RuntimeError(\"Host runtime is already started.\")\n        self._serve_task = asyncio.create_task(self._serve())\n\n    async def stop(self, grace: int = 5) -> None:\n        \"\"\"Stop the server.\"\"\"\n        if self._serve_task is None:\n            raise RuntimeError(\"Host runtime is not started.\")\n        await self._server.stop(grace=grace)\n        self._serve_task.cancel()\n        try:\n            await self._serve_task\n        except asyncio.CancelledError:\n            pass\n        logger.info(\"Server stopped.\")\n        self._serve_task = None\n\n    async def stop_when_signal(\n        self, grace: int = 5, signals: Sequence[signal.Signals] = (signal.SIGTERM, signal.SIGINT)\n    ) -> None:","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime_host.py#L18-L54","documentation":"GrpcWorkerAgentRuntimeHost.start() launches the gRPC server as a background asyncio task and guards against double-start: if self._serve_task is already set it raises RuntimeError('Host runtime is already started.'). The flag is only cleared by stop(), so calling start() twice on a live host is rejected.","triggerScenarios":"Calling host.start() a second time before host.stop(); re-running setup code (notebook cell, test fixture) against a host object that is still serving; orchestration code that retries initialization by calling start() again after a partial failure where the task was already created.","commonSituations":"Notebook cells re-executed without recreating the host; pytest fixtures calling start() per test on a session-scoped host; retry wrappers around startup logic that treat any exception as 'try start again'.","solutions":["Call start() exactly once per host lifecycle; track started state and skip if already running","Recreate the host object (GrpcWorkerAgentRuntimeHost(address)) for a fresh start instead of re-starting","In fixtures, pair start()/stop() with setup/teardown so each test gets a stopped or fresh host","Guard with 'if host._serve_task is None' equivalent state you maintain, or try/except RuntimeError as a no-op signal"],"exampleFix":"# before\nhost.start()\nhost.start()  # RuntimeError: Host runtime is already started.\n\n# after\nhost.start()\n# ... later, to restart:\nawait host.stop()\nhost.start()","handlingStrategy":"validation","validationCode":"def start_once(host) -> None:\n    if getattr(host, '_serve_task', None) is None:\n        host.start()","typeGuard":null,"tryCatchPattern":"try:\n    host.start()\nexcept RuntimeError as e:\n    if 'already started' in str(e):\n        pass  # idempotent start\n    else:\n        raise","preventionTips":["Call start() once per host lifecycle; track state explicitly","Pair start/stop in fixtures rather than re-starting","Restart cycles go start -> stop -> start, never start -> start","Keep host creation and start adjacent in code so re-runs recreate both"],"tags":["lifecycle","grpc","duplicate-start","host"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}