microsoft/autogen · error · InvalidOperationException

Application is already running.

Error message

Application is already running.

What it means

Thrown by AgentsApp.StartAsync when Interlocked.Exchange on runningCount shows the app is already running. StartAsync is not idempotent across concurrent or repeated calls; the flag is only reset by ShutdownAsync, and PublishMessageAsync auto-starts the app, which is a frequent hidden caller.

Source

Thrown at dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs:109

    public AgentsApp(IHost host)
    {
        this.Host = host;
    }

    public IHost Host { get; private set; }

    public IServiceProvider Services => this.Host.Services;

    public IHostApplicationLifetime ApplicationLifetime => this.Services.GetRequiredService<IHostApplicationLifetime>();

    public IAgentRuntime AgentRuntime => this.Services.GetRequiredService<IAgentRuntime>();

    private int runningCount;
    public async ValueTask StartAsync()
    {
        if (Interlocked.Exchange(ref this.runningCount, 1) != 0)
        {
            throw new InvalidOperationException("Application is already running.");
        }

        Debug.Assert(this.AgentRuntime != null);

        await this.Host.StartAsync();
    }

    public async ValueTask ShutdownAsync()
    {
        if (Interlocked.Exchange(ref this.runningCount, 0) != 1)
        {
            throw new InvalidOperationException("Application is already stopped.");
        }

        await this.Host.StopAsync();
    }

    public async ValueTask PublishMessageAsync<TMessage>(TMessage message, TopicId topic, string? messageId = null, CancellationToken cancellationToken = default)

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Remove the redundant StartAsync call — check a running state flag or rely on PublishMessageAsync's auto-start
  2. Ensure AgentsApp is a single instance (correct singleton DI registration) so multiple consumers share one running flag
  3. Wrap startup in a single owned code path (one composition root) that starts the app exactly once

Example fix

// before
await app.PublishMessageAsync(msg, topic); // auto-starts
await app.StartAsync(); // throws

// after
await app.PublishMessageAsync(msg, topic); // auto-starts; no explicit StartAsync
Defensive patterns

Strategy: validation

Validate before calling

// Expose/track running state before calling start
if (appRunning) { /* skip StartAsync */ } else { await app.StartAsync(); appRunning = true; }

Try / catch

try { await app.StartAsync(); } catch (InvalidOperationException ex) when (ex.Message == "Application is already running.") { _logger.LogDebug("App already started; continuing"); }

Prevention

When it happens

Trigger: Calling app.StartAsync() manually when PublishMessageAsync already auto-started it; double-start in host wiring (e.g. explicit StartAsync plus a hosted service start); concurrent startup paths racing the runningCount flag.

Common situations: Mixing explicit StartAsync with implicit auto-start from publish; DI lifetimes producing two AgentsApp usages that both start; startup code refactored so StartAsync runs twice on the same instance.

Related errors


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