{"record":{"id":"eff83aada82782fe","repo":"microsoft/aspire","slug":"request-was-cancelled","errorCode":null,"errorMessage":"Request was cancelled","messagePattern":"Request was cancelled","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs","lineNumber":1193,"sourceCode":"                }\n                # Create event for response\n                event = threading.Event()\n                with self._lock:\n                    self._pending_requests[request_id] = event\n\n                # Send request\n                self._send_message(request)\n\n                # Wait for response\n                event.wait()\n\n                # Get result\n                with self._lock:\n                    if request_id not in self._pending_results:\n                        # Request was cancelled/interrupted\n                        if self._connection_error:\n                            raise self._connection_error\n                        raise RuntimeError(\"Request was cancelled\")\n                    del self._pending_requests[request_id]\n                    result, error = self._pending_results.pop(request_id)\n\n                if error:\n                    raise error\n\n                return result\n\n            def register_cancellation_token(self, cancellation_timeout: int | None) -> str | None:\n                if not cancellation_timeout:\n                    return None\n\n                with self._lock:\n                    cancellation_id = f\"ct_{self._cancellation_id}_{int(time.time() * 1000)}\"\n                    cancellation_token = threading.Event()\n\n                    def cancellation_thread():\n                        cancellation_token.wait()","sourceCodeStart":1175,"sourceCodeEnd":1211,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs#L1175-L1211","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Retry the request after ensuring the client is still connected and the AppHost responsive.","Avoid closing the client or cancelling the calling task while requests are pending.","Send fewer concurrent requests or serialize access if a race on pending state is suspected.","Upgrade/regenerate the module if this occurs deterministically — it may indicate a client bug losing results."],"exampleFix":"// before\nthreading.Thread(target=client.close).start()  # closes mid-request\nresult = client.invoke_capability(\"build\", {})\n// after\nresult = client.invoke_capability(\"build\", {})\nclient.close()  # close only after pending requests finish","handlingStrategy":"try-catch","validationCode":"def safe_invoke(client, cap, args, retries=1):\n    for attempt in range(retries + 1):\n        try:\n            return client.invoke_capability(cap, args)\n        except RuntimeError as e:\n            if 'Request was cancelled' in str(e) and attempt < retries:\n                continue\n            raise","typeGuard":null,"tryCatchPattern":"try:\n    result = client.invoke_capability(cap, args)\nexcept RuntimeError as e:\n    if 'Request was cancelled' in str(e):\n        result = retry_request(cap, args)\n    else:\n        raise","preventionTips":["Do not close the client or cancel tasks while requests are pending","Bound concurrency so pending-state races are unlikely","Retry once on this error before surfacing it"],"tags":["python","cancellation","concurrency","rpc"],"backgroundTag":"thread-interrupted","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}