microsoft/semantic-kernel · error · RuntimeError

Runtime is not started

Error message

Runtime is not started

What it means

InProcessRuntime.stop() requires an active run context (_run_context is not None). If the runtime was never started (or already stopped), _run_context is None and stop() raises RuntimeError. This is the immediate-stop variant.

Source

Thrown at python/semantic_kernel/agents/runtime/in_process/in_process_runtime.py:674

        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:
            await self._run_context.stop()
        finally:
            self._run_context = None
            self._message_queue = Queue()

    async def stop_when_idle(self) -> None:
        """Stop the runtime message processing loop when there is no outstanding message being processed or queued.

        This is the most common way to stop the runtime.
        """
        if self._run_context is None:
            raise RuntimeError("Runtime is not started")

        try:
            await self._run_context.stop_when_idle()
        finally:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Guard the stop call: check runtime._run_context is not None, or prefer runtime.close() which handles this internally.
  2. Ensure start() is called and awaited before any stop call.
  3. Use try/finally around start/stop pairs so stop is only reached if start succeeded.

Example fix

# before
await runtime.stop()  # raises if never started

# after
if runtime._run_context is not None:
    await runtime.stop()
# or simply:
await runtime.close()  # internally guards
Defensive patterns

Strategy: validation

Validate before calling

# Guard stop() calls
if runtime._run_context is not None:
    await runtime.stop()
# Or use close() which guards internally:
await runtime.close()

Type guard

null

Try / catch

try:
    await runtime.stop()
except RuntimeError:
    pass  # already stopped — not an error condition

Prevention

When it happens

Trigger: Calling await runtime.stop() before runtime.start(), or calling stop() after the runtime was already stopped (double-stop). The close() method guards against this by checking _run_context before calling stop().

Common situations: Test teardown calling stop() unconditionally. Application shutdown logic calling stop() defensively when the runtime may or may not have been started.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/4b9491003ee63d8b. Report an issue: GitHub.