microsoft/aspire · error · RuntimeError

Request was cancelled

Error message

Request was cancelled

What it means

When awaiting a request result, if the request_id has no entry in _pending_results the client raises RuntimeError('Request was cancelled'), meaning the request was interrupted (e.g., the receive loop terminated or the wait was cancelled) before a result arrived. If a stored connection error exists, that error is raised instead, so this message implies an interruption without a recorded connection failure.

Solutions

  1. Retry the request after ensuring the client is still connected and the AppHost responsive.
  2. Avoid closing the client or cancelling the calling task while requests are pending.
  3. Send fewer concurrent requests or serialize access if a race on pending state is suspected.
  4. Upgrade/regenerate the module if this occurs deterministically — it may indicate a client bug losing results.

Example fix

// before
threading.Thread(target=client.close).start()  # closes mid-request
result = client.invoke_capability("build", {})
// after
result = client.invoke_capability("build", {})
client.close()  # close only after pending requests finish
Defensive patterns

Strategy: try-catch

Validate before calling

def safe_invoke(client, cap, args, retries=1):
    for attempt in range(retries + 1):
        try:
            return client.invoke_capability(cap, args)
        except RuntimeError as e:
            if 'Request was cancelled' in str(e) and attempt < retries:
                continue
            raise

Try / catch

try:
    result = client.invoke_capability(cap, args)
except RuntimeError as e:
    if 'Request was cancelled' in str(e):
        result = retry_request(cap, args)
    else:
        raise

Prevention

When it happens

Trigger: Concurrent cancellation/interruption of a pending request; the response path dropped the pending result; calling from a thread/task that was cancelled while waiting; receive-loop shutdown between sending the request and reading its reply.

Common situations: KeyboardInterrupt or asyncio cancellation during a long invoke_capability call; closing/disconnecting the client from another thread mid-request; app shutdown while requests are in flight.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/eff83aada82782fe. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs:1193

                }
                # Create event for response
                event = threading.Event()
                with self._lock:
                    self._pending_requests[request_id] = event

                # Send request
                self._send_message(request)

                # Wait for response
                event.wait()

                # Get result
                with self._lock:
                    if request_id not in self._pending_results:
                        # Request was cancelled/interrupted
                        if self._connection_error:
                            raise self._connection_error
                        raise RuntimeError("Request was cancelled")
                    del self._pending_requests[request_id]
                    result, error = self._pending_results.pop(request_id)

                if error:
                    raise error

                return result

            def register_cancellation_token(self, cancellation_timeout: int | None) -> str | None:
                if not cancellation_timeout:
                    return None

                with self._lock:
                    cancellation_id = f"ct_{self._cancellation_id}_{int(time.time() * 1000)}"
                    cancellation_token = threading.Event()

                    def cancellation_thread():
                        cancellation_token.wait()

View on GitHub (pinned to 25830f84bd)