microsoft/autogen · error · RuntimeError

Runtime is already started

Error message

Runtime is already started

What it means

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.

Source

Thrown at python/packages/autogen-core/src/autogen_core/_single_threaded_agent_runtime.py:820

            import asyncio
            from autogen_core import SingleThreadedAgentRuntime


            async def main() -> None:
                runtime = SingleThreadedAgentRuntime()
                runtime.start()

                # ... do other things ...

                await runtime.stop()


            asyncio.run(main())

        """
        if self._run_context is not None:
            raise RuntimeError("Runtime is already started")
        self._run_context = RunContext(self)

    async def close(self) -> None:
        """Calls :meth:`stop` if applicable and the :meth:`Agent.close` method on all instantiated agents"""
        # stop the runtime if it hasn't been stopped yet
        if self._run_context is not None:
            await self.stop()
        # close all the agents that have been instantiated
        for agent_id in self._instantiated_agents:
            agent = await self._get_agent(agent_id)
            await agent.close()

    async def stop(self) -> None:
        """Immediately stop the runtime message processing loop. The currently processing message will be completed, but all others following it will be discarded."""
        if self._run_context is None:
            raise RuntimeError("Runtime is not started")

        try:

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. 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)
  2. Use stop_when_idle() + start() cycle per phase instead of multiple starts
  3. Create a fresh SingleThreadedAgentRuntime instance for each run session rather than restarting the same one

Example fix

# before
runtime.start()
runtime.start()  # RuntimeError

# after
runtime.start()
...
await runtime.stop_when_idle()
runtime.start()  # now valid
Defensive patterns

Strategy: validation

Validate before calling

def start_once(runtime):
    if runtime._run_context is None:
        runtime.start()
        return True
    return False

Try / catch

try:
    runtime.start()
except RuntimeError as e:
    if "already started" in str(e):
        pass  # treat as success
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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