github/copilot-sdk · error · RuntimeError
Copilot request was cancelled by the runtime.
Error message
Copilot request was cancelled by the runtime.
What it means
write_response() refuses to write body data once the runtime has cancelled the Copilot request (the `cancelled` flag was set via the cancellation path). Continuing to write would stream bytes to a request_id the client/runtime has abandoned. The library raises immediately so the handler can stop producing output.
Solutions
- Check `response.cancelled` before each write (or wrap writes in try/except RuntimeError and break the stream loop).
- Stop generation as soon as cancellation is observed; do not attempt further writes or end_response().
- Register a cancellation callback that terminates the upstream model stream so writes never happen post-cancel.
Example fix
// before
for chunk in model_stream:
await response.write_response(chunk)
// after
for chunk in model_stream:
if response.cancelled:
break
try:
await response.write_response(chunk)
except RuntimeError:
break Defensive patterns
Strategy: try-catch
Validate before calling
if response.cancelled:
stop_generation()
return Type guard
def is_writable(response) -> bool:
return not response.cancelled and response.started and not response.finished Try / catch
try:
await response.write_response(chunk)
except RuntimeError as e:
if "cancelled by the runtime" in str(e):
break # client/runtime gone; stop streaming
raise Prevention
- Check `cancelled` before every write in streaming loops.
- Wire cancellation into the upstream generator so it stops producing immediately.
- Never treat post-cancel writes as recoverable; abort the stream.
When it happens
Trigger: The runtime cancelled the request (client disconnect, timeout, explicit cancel) and the handler subsequently calls write_response(); long-running generation continues streaming after a cancel notification arrives.
Common situations: User closes the connection or hits stop mid-generation while a model callback keeps streaming; timeouts in the runtime cancelling the exchange while the handler loop is mid-chunk.
Related errors
- Request cancelled by runtime
- Request cancelled by runtime
- Copilot request was cancelled by the runtime.
- Request cancelled by runtime
- LLM inference request was cancelled by the runtime
AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09).
Data as JSON: /api/errors/323698eeae6d59bb.
Report an issue: GitHub.
Appendix: source
Thrown at python/copilot/copilot_request_handler.py:456
headers: LlmInferenceHeaders | None = None,
) -> None:
if self.started:
raise RuntimeError("Copilot request response start() called twice.")
if self.finished:
raise RuntimeError("Copilot request response already finished.")
self.started = True
await self._require_rpc().http_response_start(
LlmInferenceHTTPResponseStartRequest(
headers=headers or {},
request_id=self.request_id,
status=status,
status_text=status_text,
)
)
async def write_response(self, data: str | bytes) -> None:
if self.cancelled:
raise RuntimeError("Copilot request was cancelled by the runtime.")
if not self.started:
raise RuntimeError("Copilot request response write() called before start().")
if self.finished:
raise RuntimeError("Copilot request response write() called after end()/error().")
if isinstance(data, bytes):
payload = base64.b64encode(data).decode("ascii")
is_binary = True
else:
payload = data
is_binary = False
await self._require_rpc().http_response_chunk(
LlmInferenceHTTPResponseChunkRequest(
data=payload,
request_id=self.request_id,
binary=is_binary or None,
end=False,
)
)View on GitHub (pinned to cd8cf15dc3)