microsoft/aspire · info · OperationCancelled

OperationCancelled( )

Error message

OperationCancelled({self.handle.handle_id})

What it means

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.

Solutions

  1. Catch OperationCancelled around cancel() and the awaited operation — treat it as expected control flow.
  2. Do not interpret it as a server failure; verify server-side cancellation if needed via subsequent calls.
  3. Structure long-running calls so cancellation handling is centralized in one except clause.
  4. If you only wanted server-side signalling without the local exception, wrap cancel() in try/except that swallows OperationCancelled.

Example fix

// before
token.cancel()  # raises OperationCancelled, crashes script
// after
try:
    token.cancel()
except OperationCancelled:
    pass  # expected: cancellation signal delivered
Defensive patterns

Strategy: try-catch

Try / catch

try:
    token.cancel()
except OperationCancelled:
    pass  # expected control flow after cancelling

Prevention

When it happens

Trigger: Explicitly calling token.cancel() on a CancellationToken obtained from the client; any code path that cancels a long-running capability operation.

Common situations: 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.

Related errors


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

Appendix: source

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


        # ============================================================================
        # CancellationToken
        # ============================================================================

        class CancellationToken:
            '''Represents a cancellation token that can be used to cancel a callback in progress.'''

            handle: Handle

            def __init__(self, handle: Handle, client: AspireClient) -> None:
                self.handle = handle
                self._client = client
            
            def cancel(self) -> None:
                '''Cancel the token, which will signal the server to cancel the associated operation.'''
                self._client._send_request("cancelToken", self.handle.handle_id)
                raise OperationCancelled(self.handle.handle_id)


        # ============================================================================
        # Reference Expression
        # ============================================================================

        class ReferenceExpression:
            '''Represents a reference expression passed to capabilities.
            Supports both value mode (format + valueProviders) and conditional mode (condition + whenTrue + whenFalse).
            '''

            def __init__(self, handle: Handle | None, **kwargs) -> None:
                '''
                Creates a reference expression from a format string and value providers.

                Args:
                    format_str: Format string with {0}, {1}, etc. placeholders
                    *value_providers: Handles to value providers

View on GitHub (pinned to 25830f84bd)