{"record":{"id":"6c350fe97ec35f5f","repo":"microsoft/aspire","slug":"operationcancelled-self-handle-handle-id","errorCode":null,"errorMessage":"OperationCancelled({self.handle.handle_id})","messagePattern":"OperationCancelled\\((.+?)\\)","errorType":"exception","errorClass":"OperationCancelled","httpStatus":null,"severity":"info","filePath":"src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs","lineNumber":1331,"sourceCode":"\n\n        # ============================================================================\n        # CancellationToken\n        # ============================================================================\n\n        class CancellationToken:\n            '''Represents a cancellation token that can be used to cancel a callback in progress.'''\n\n            handle: Handle\n\n            def __init__(self, handle: Handle, client: AspireClient) -> None:\n                self.handle = handle\n                self._client = client\n            \n            def cancel(self) -> None:\n                '''Cancel the token, which will signal the server to cancel the associated operation.'''\n                self._client._send_request(\"cancelToken\", self.handle.handle_id)\n                raise OperationCancelled(self.handle.handle_id)\n\n\n        # ============================================================================\n        # Reference Expression\n        # ============================================================================\n\n        class ReferenceExpression:\n            '''Represents a reference expression passed to capabilities.\n            Supports both value mode (format + valueProviders) and conditional mode (condition + whenTrue + whenFalse).\n            '''\n\n            def __init__(self, handle: Handle | None, **kwargs) -> None:\n                '''\n                Creates a reference expression from a format string and value providers.\n\n                Args:\n                    format_str: Format string with {0}, {1}, etc. placeholders\n                    *value_providers: Handles to value providers","sourceCodeStart":1313,"sourceCodeEnd":1349,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs#L1313-L1349","documentation":"CancellationToken.cancel() sends a 'cancelToken' request to the AppHost and then unconditionally raises OperationCancelled carrying the handle id, signalling to local callers that the associated operation has been (asynchronously) cancelled. This is a control-flow exception, raised by design on every cancel() call — it is the library's way of unwinding code waiting on the token.","triggerScenarios":"Explicitly calling token.cancel() on a CancellationToken obtained from the client; any code path that cancels a long-running capability operation.","commonSituations":"User-initiated abort of a long operation; timeout wrappers that cancel the token; scripts that cancel and do not expect an exception from cancel() itself.","solutions":["Catch OperationCancelled around cancel() and the awaited operation — treat it as expected control flow.","Do not interpret it as a server failure; verify server-side cancellation if needed via subsequent calls.","Structure long-running calls so cancellation handling is centralized in one except clause.","If you only wanted server-side signalling without the local exception, wrap cancel() in try/except that swallows OperationCancelled."],"exampleFix":"// before\ntoken.cancel()  # raises OperationCancelled, crashes script\n// after\ntry:\n    token.cancel()\nexcept OperationCancelled:\n    pass  # expected: cancellation signal delivered","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    token.cancel()\nexcept OperationCancelled:\n    pass  # expected control flow after cancelling","preventionTips":["Always wrap cancel() in try/except OperationCancelled","Centralize cancellation handling in one helper","Never treat OperationCancelled as a server failure"],"tags":["python","cancellation","control-flow","exception"],"backgroundTag":"operation-not-supported","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"}