microsoft/autogen · error · RuntimeError

Host connection is not set.

Error message

Host connection is not set.

What it means

GrpcWorkerAgentRuntime._send_message sends a protobuf runtime message over the host connection; if the internal _host_connection is None (runtime never started, or connection already torn down) it raises RuntimeError('Host connection is not set.'). This is the internal chokepoint used by both send_message and publish_message paths.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py:362

        # Wait for the signal to trigger the shutdown event.
        await shutdown_event.wait()

        # Stop the runtime.
        await self.stop()

    @property
    def _known_agent_names(self) -> Set[str]:
        return set(self._agent_factories.keys())

    async def _send_message(
        self,
        runtime_message: agent_worker_pb2.Message,
        send_type: Literal["send", "publish"],
        recipient: AgentId | TopicId,
        telemetry_metadata: Mapping[str, str],
    ) -> None:
        if self._host_connection is None:
            raise RuntimeError("Host connection is not set.")
        with self._trace_helper.trace_block(send_type, recipient, parent=telemetry_metadata):
            await self._host_connection.send(runtime_message)

    async def send_message(
        self,
        message: Any,
        recipient: AgentId,
        *,
        sender: AgentId | None = None,
        cancellation_token: CancellationToken | None = None,
        message_id: str | None = None,
    ) -> Any:
        # TODO: use message_id
        if not self._running:
            raise ValueError("Runtime must be running when sending message.")
        if self._host_connection is None:
            raise RuntimeError("Host connection is not set.")
        data_type = self._serialization_registry.type_name(message)

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Ensure `await runtime.stop()` has drained in-flight sends (stop() gathers background tasks) before closing/awaiting further operations
  2. Never fire publish_message/send_message after stop(); check runtime state before enqueueing work
  3. Catch RuntimeError in wrappers that may race shutdown and treat it as a cancellation

Example fix

# before
task = asyncio.create_task(publish_later(runtime))
await runtime.stop()  # task may then hit 'Host connection is not set.'

# after
await runtime.stop()  # stop() drains background tasks first
task.cancel()
Defensive patterns

Strategy: try-catch

Validate before calling

# Avoid hitting it: never enqueue sends after stop(); ensure background tasks are awaited by stop() first
if shutting_down.is_set():
    raise OperationRejected("runtime shutting down")

Try / catch

try:
    await runtime.publish_message(msg, topic)
except RuntimeError as e:
    if "Host connection is not set" in str(e):
        log.warning("send after shutdown dropped")
    else:
        raise

Prevention

When it happens

Trigger: A background publish/send task created before stop() completes executes after the host connection is closed, or calling the runtime's messaging APIs from a path that bypassed start() — the public methods also check _running first, so this is typically seen from fire-and-forget tasks racing stop().

Common situations: Publishing messages from tasks that outlive runtime.stop(); awaiting runtime.stop() while background tasks queued via _background_tasks are still sending; tests that construct the runtime and call internals directly.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/6f1682d4fd44d086. Report an issue: GitHub.