github/copilot-sdk · warning · RuntimeError

Request cancelled by runtime

Error message

Request cancelled by runtime: {item.cancel_reason}

What it means

During async iteration of the model response stream (__anext__), the runtime delivered a cancellation item. The handler converts it into a RuntimeError carrying the runtime-provided cancel_reason (or a generic message when none is given). Iteration cannot continue after this point.

Solutions

  1. Catch RuntimeError around the async-for loop and treat it as a normal cancellation, not a bug
  2. Inspect the cancel_reason embedded in the message to decide whether to retry or surface to the user
  3. Stop consuming the iterator after cancellation; set self._done semantics hold — a fresh request is needed
  4. Implement retry with backoff for transient cancellation causes like timeouts

Example fix

// before
text = ""
async for chunk in response:
    text += chunk
// after
text = ""
try:
    async for chunk in response:
        text += chunk
except RuntimeError as e:
    if "Request cancelled by runtime" in str(e):
        text = None  # handle cancellation gracefully
Defensive patterns

Strategy: try-catch

Try / catch

try:
    async for chunk in response:
        process(chunk)
except RuntimeError as e:
    if str(e).startswith("Request cancelled by runtime"):
        handle_cancel(reason=str(e).partition(": ")[2])
    else:
        raise

Prevention

When it happens

Trigger: The Copilot runtime cancels an in-flight streaming request — e.g. user aborts, upstream timeout, or the server cancels the turn — and the cancel item reaches __anext__ with (or without) a cancel_reason.

Common situations: User pressing stop in an IDE/chat UI, long-running prompts killed by server-side limits, network drop causing the runtime to cancel, or client shutdown mid-stream.

Understand the failure class

Background: Request timed out: what client-side request timeouts mean across libraries (Request timed out, TIMED_OUT, APITimeoutError) — this error's family across 39 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/2e3909341ffb6a6b. Report an issue: GitHub.

Appendix: source

Thrown at python/copilot/copilot_request_handler.py:374

    def push(self, item: _BodyItem) -> None:
        self._queue.put_nowait(item)

    def __aiter__(self) -> AsyncIterator[bytes]:
        return self

    async def __anext__(self) -> bytes:
        if self._done:
            raise StopAsyncIteration
        item = await self._queue.get()
        if item.cancel:
            self._done = True
            reason = (
                f"Request cancelled by runtime: {item.cancel_reason}"
                if item.cancel_reason
                else "Request cancelled by runtime"
            )
            raise RuntimeError(reason)
        if item.end:
            self._done = True
            raise StopAsyncIteration
        return item.chunk if item.chunk is not None else b""


class _CopilotRequestExchange:
    """One intercepted request in flight.

    Carries the request body stream the runtime feeds via ``httpRequestChunk``
    frames, and emits the handler's response directly to the runtime through
    the generated ``llmInference`` RPC. Replaces the former provider / sink /
    response-channel indirection with a single object the adapter owns.
    """

    def __init__(
        self,
        request_id: str,

View on GitHub (pinned to cd8cf15dc3)