microsoft/autogen · error · AggregateException

One or more exceptions occurred while processing the message

Error message

One or more exceptions occurred while processing the message.

What it means

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.

Source

Thrown at dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs:84

                {
                    continue;
                }

                IHostableAgent agent = await this.EnsureAgentAsync(agentId);

                // TODO: Cancellation propagation!
                await agent.OnMessageAsync(envelope.Message, messageContext);
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
        }

        if (exceptions.Count > 0)
        {
            // TODO: Unwrap TargetInvocationException?
            throw new AggregateException("One or more exceptions occurred while processing the message.", exceptions);
        }
    }

    public ValueTask PublishMessageAsync(object message, TopicId topic, AgentId? sender = null, string? messageId = null, CancellationToken cancellation = default)
    {
        return this.ExecuteTracedAsync(() =>
        {
            MessageDelivery delivery = new MessageEnvelope(message, messageId, cancellation)
                                        .WithSender(sender)
                                        .ForPublish(topic, this.PublishMessageServicer);

            this.messageDeliveryQueue.Enqueue(delivery);

            return delivery.FutureNoResult;
        });
    }

    private async ValueTask<object?> SendMessageServicer(MessageEnvelope envelope, CancellationToken deliveryToken)

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Inspect AggregateException.Flatten().InnerExceptions to find the actual failing agent and root cause — the outer message is generic.
  2. Fix the bug in the throwing agent's OnMessageAsync (the inner exception points at it).
  3. If several InnerExceptions appear, identify which agents share the topic subscription that produced them.
  4. 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.

Example fix

// before
try
{
    await runtime.PublishMessageAsync(new MyEvent(), topic);
}
catch (Exception ex)
{
    _logger.LogError(ex, "publish failed"); // logs the generic AggregateException
}

// after
try
{
    await runtime.PublishMessageAsync(new MyEvent(), topic);
}
catch (AggregateException ae)
{
    foreach (var inner in ae.Flatten().InnerExceptions)
    {
        _logger.LogError(inner, "agent delivery failed: {Type}", inner.GetType().Name);
    }
}
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    await runtime.PublishMessageAsync(message, topic);
}
catch (AggregateException ae)
{
    foreach (var inner in ae.Flatten().InnerExceptions)
    {
        logger.LogError(inner, "Agent message delivery failed: {Type}", inner.GetType().Name);
    }
    // optionally rethrow the single inner exception when InnerExceptions.Count == 1
}

Prevention

When it happens

Trigger: 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.

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

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/8430379af1232e7b. Report an issue: GitHub.