{"record":{"id":"9ea19d1e5d3bfad9","repo":"microsoft/semantic-kernel","slug":"delivery-of-message-messageid-was-cancelled","errorCode":null,"errorMessage":"Delivery of message {messageId} was cancelled.","messagePattern":"Delivery of message (.+?) was cancelled\\.","errorType":"exception","errorClass":"OperationCanceledException","httpStatus":null,"severity":"warning","filePath":"dotnet/src/Agents/Runtime/InProcess/InProcessRuntime.cs","lineNumber":142,"sourceCode":"    public async ValueTask<object?> SendMessageAsync(object message, AgentId recipient, AgentId? sender = null, string? messageId = null, CancellationToken cancellationToken = default)\n    {\n        return await this.ExecuteTracedAsync(async () =>\n        {\n            MessageDelivery delivery =\n                new MessageEnvelope(message, messageId, cancellationToken)\n                    .WithSender(sender)\n                    .ForSend(recipient, this.SendMessageServicerAsync);\n\n            this._messageDeliveryQueue.Enqueue(delivery);\n            Interlocked.Increment(ref this.messageQueueCount);\n\n            try\n            {\n                return await delivery.ResultSink.Future.ConfigureAwait(false);\n            }\n            catch (TargetInvocationException ex) when (ex.InnerException is OperationCanceledException innerOCEx)\n            {\n                throw new OperationCanceledException($\"Delivery of message {messageId} was cancelled.\", innerOCEx);\n            }\n        }).ConfigureAwait(false);\n    }\n\n    /// <inheritdoc/>\n    public async ValueTask<AgentId> GetAgentAsync(AgentId agentId, bool lazy = true)\n    {\n        if (!lazy)\n        {\n            await this.EnsureAgentAsync(agentId).ConfigureAwait(false);\n        }\n\n        return agentId;\n    }\n\n    /// <inheritdoc/>\n    public ValueTask<AgentId> GetAgentAsync(AgentType agentType, string key = AgentId.DefaultKey, bool lazy = true)\n        => this.GetAgentAsync(new AgentId(agentType, key), lazy);","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Runtime/InProcess/InProcessRuntime.cs#L124-L160","documentation":"InProcessRuntime.SendMessageAsync awaits the delivery's ResultSink.Future; if it faults with TargetInvocationException wrapping OperationCanceledException, the catch unwraps and rethrows a new OperationCanceledException naming the messageId. This is the normal propagation when a message delivery is cancelled (explicit token, agent-side cancellation, or runtime shutdown).","triggerScenarios":"The caller's CancellationToken passed to SendMessageAsync is cancelled mid-delivery; the runtime's shutdown token (_shutdownSource) cancels pending deliveries; the receiving agent throws OperationCanceledException; StopAsync fires while an RPC send is in flight.","commonSituations":"Request timeouts implemented via CancellationTokenSource.CancelAfter; user-initiated cancel; graceful shutdown that aborts in-flight sends; an agent whose handler honors cancellation.","solutions":["If cancellation is expected, catch OperationCanceledException and treat it as non-fatal.","Ensure your CancellationToken is not cancelled prematurely (check timeout/CancellationTokenSource configuration).","Avoid calling StopAsync while RPC sends are still awaited if you need their results."],"exampleFix":"// before\nobject? reply = await runtime.SendMessageAsync(msg, recipient, cancellationToken: cts.Token);\n\n// after\ntry\n{\n    object? reply = await runtime.SendMessageAsync(msg, recipient, cancellationToken: cts.Token);\n}\ncatch (OperationCanceledException) when (cts.IsCancellationRequested)\n{\n    // expected cancellation; handle gracefully\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { object? reply = await runtime.SendMessageAsync(msg, recipient, cancellationToken: ct); }\ncatch (OperationCanceledException) when (ct.IsCancellationRequested)\n{\n    // expected cancellation; do not treat as fatal\n}","preventionTips":["Do not cancel the token if you still need the send's result.","Avoid StopAsync while RPC sends are in flight.","Distinguish expected cancellation (timeouts) from unexpected agent-side cancellation."],"tags":["inprocess-runtime","messaging","cancellation","send","dotnet"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}