{"record":{"id":"b1720403df7eead3","repo":"microsoft/autogen","slug":"application-is-already-running","errorCode":null,"errorMessage":"Application is already running.","messagePattern":"Application is already running\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs","lineNumber":109,"sourceCode":"    public AgentsApp(IHost host)\n    {\n        this.Host = host;\n    }\n\n    public IHost Host { get; private set; }\n\n    public IServiceProvider Services => this.Host.Services;\n\n    public IHostApplicationLifetime ApplicationLifetime => this.Services.GetRequiredService<IHostApplicationLifetime>();\n\n    public IAgentRuntime AgentRuntime => this.Services.GetRequiredService<IAgentRuntime>();\n\n    private int runningCount;\n    public async ValueTask StartAsync()\n    {\n        if (Interlocked.Exchange(ref this.runningCount, 1) != 0)\n        {\n            throw new InvalidOperationException(\"Application is already running.\");\n        }\n\n        Debug.Assert(this.AgentRuntime != null);\n\n        await this.Host.StartAsync();\n    }\n\n    public async ValueTask ShutdownAsync()\n    {\n        if (Interlocked.Exchange(ref this.runningCount, 0) != 1)\n        {\n            throw new InvalidOperationException(\"Application is already stopped.\");\n        }\n\n        await this.Host.StopAsync();\n    }\n\n    public async ValueTask PublishMessageAsync<TMessage>(TMessage message, TopicId topic, string? messageId = null, CancellationToken cancellationToken = default)","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs#L91-L127","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove the redundant StartAsync call — check a running state flag or rely on PublishMessageAsync's auto-start","Ensure AgentsApp is a single instance (correct singleton DI registration) so multiple consumers share one running flag","Wrap startup in a single owned code path (one composition root) that starts the app exactly once"],"exampleFix":"// before\nawait app.PublishMessageAsync(msg, topic); // auto-starts\nawait app.StartAsync(); // throws\n\n// after\nawait app.PublishMessageAsync(msg, topic); // auto-starts; no explicit StartAsync","handlingStrategy":"validation","validationCode":"// Expose/track running state before calling start\nif (appRunning) { /* skip StartAsync */ } else { await app.StartAsync(); appRunning = true; }","typeGuard":null,"tryCatchPattern":"try { await app.StartAsync(); } catch (InvalidOperationException ex) when (ex.Message == \"Application is already running.\") { _logger.LogDebug(\"App already started; continuing\"); }","preventionTips":["Own the start in a single composition root","Remember PublishMessageAsync auto-starts the app","Register AgentsApp as a singleton"],"tags":["lifecycle","startup","concurrency","double-start"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}