{"record":{"id":"8430379af1232e7b","repo":"microsoft/autogen","slug":"one-or-more-exceptions-occurred-while-processing-t","errorCode":null,"errorMessage":"One or more exceptions occurred while processing the message.","messagePattern":"One or more exceptions occurred while processing the message\\.","errorType":"exception","errorClass":"AggregateException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs","lineNumber":84,"sourceCode":"                {\n                    continue;\n                }\n\n                IHostableAgent agent = await this.EnsureAgentAsync(agentId);\n\n                // TODO: Cancellation propagation!\n                await agent.OnMessageAsync(envelope.Message, messageContext);\n            }\n            catch (Exception ex)\n            {\n                exceptions.Add(ex);\n            }\n        }\n\n        if (exceptions.Count > 0)\n        {\n            // TODO: Unwrap TargetInvocationException?\n            throw new AggregateException(\"One or more exceptions occurred while processing the message.\", exceptions);\n        }\n    }\n\n    public ValueTask PublishMessageAsync(object message, TopicId topic, AgentId? sender = null, string? messageId = null, CancellationToken cancellation = default)\n    {\n        return this.ExecuteTracedAsync(() =>\n        {\n            MessageDelivery delivery = new MessageEnvelope(message, messageId, cancellation)\n                                        .WithSender(sender)\n                                        .ForPublish(topic, this.PublishMessageServicer);\n\n            this.messageDeliveryQueue.Enqueue(delivery);\n\n            return delivery.FutureNoResult;\n        });\n    }\n\n    private async ValueTask<object?> SendMessageServicer(MessageEnvelope envelope, CancellationToken deliveryToken)","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs#L66-L102","documentation":"InProcessRuntime delivers each published message to all matching subscribers sequentially and awaits each agent's OnMessageAsync. Any exception thrown by an agent's message handler is caught and collected rather than failing the delivery loop immediately; after all deliveries for that message complete, the runtime rethrows all collected failures wrapped in a single AggregateException. This preserves the original stack traces in the InnerExceptions collection. The TODO comments in the source show cancellation propagation and TargetInvocationException unwrapping are not yet handled, so inner exceptions may be wrapped or lost cancellation context.","triggerScenarios":"Calling PublishMessageAsync (or SendMessageAsync) on InProcessRuntime where at least one subscribed agent's OnMessageAsync throws — e.g. a handler that throws ArgumentNullException, a deserialization failure inside the handler, or a handler calling an external API that faults. The throw site is the delivery-completion block in InProcessRuntime.cs:84, after the per-agent try/catch loop has accumulated at least one exception.","commonSituations":"A handler throws on bad message payload; multiple agents subscribe to the same topic and more than one fails, so the AggregateException contains several InnerExceptions; a cancelled token inside a handler surfaces as an unexpected exception because cancellation propagation is a TODO; user code catches Exception but not AggregateException and misses the real cause.","solutions":["Inspect AggregateException.Flatten().InnerExceptions to find the actual failing agent and root cause — the outer message is generic.","Fix the bug in the throwing agent's OnMessageAsync (the inner exception points at it).","If several InnerExceptions appear, identify which agents share the topic subscription that produced them.","If the inner exception is OperationCanceledException, check the TODO on cancellation propagation at the call site — the cancellation token may not be reaching the handler."],"exampleFix":"// before\ntry\n{\n    await runtime.PublishMessageAsync(new MyEvent(), topic);\n}\ncatch (Exception ex)\n{\n    _logger.LogError(ex, \"publish failed\"); // logs the generic AggregateException\n}\n\n// after\ntry\n{\n    await runtime.PublishMessageAsync(new MyEvent(), topic);\n}\ncatch (AggregateException ae)\n{\n    foreach (var inner in ae.Flatten().InnerExceptions)\n    {\n        _logger.LogError(inner, \"agent delivery failed: {Type}\", inner.GetType().Name);\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    await runtime.PublishMessageAsync(message, topic);\n}\ncatch (AggregateException ae)\n{\n    foreach (var inner in ae.Flatten().InnerExceptions)\n    {\n        logger.LogError(inner, \"Agent message delivery failed: {Type}\", inner.GetType().Name);\n    }\n    // optionally rethrow the single inner exception when InnerExceptions.Count == 1\n}","preventionTips":["Make agent OnMessageAsync handlers defensive: validate payloads and catch domain-specific failures inside the handler","Log the correlation/messageId with each delivery so an AggregateException can be traced to the failing agent","When multiple agents subscribe to one topic, remember any subset may fail and all failures arrive in one AggregateException"],"tags":["csharp","runtime","message-delivery","aggregate-exception","inprocess"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}