{"record":{"id":"05c0b79bb0d6935c","repo":"microsoft/semantic-kernel","slug":"runtime-is-already-running","errorCode":null,"errorMessage":"Runtime is already running.","messagePattern":"Runtime is already running\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Runtime/InProcess/InProcessRuntime.cs","lineNumber":56,"sourceCode":"    /// <inheritdoc/>\n    public async ValueTask DisposeAsync()\n    {\n        await this.RunUntilIdleAsync().ConfigureAwait(false);\n        this._shutdownSource?.Dispose();\n        this._finishSource?.Dispose();\n    }\n\n    /// <summary>\n    /// Starts the runtime service.\n    /// </summary>\n    /// <param name=\"cancellationToken\">Token to monitor for shutdown requests.</param>\n    /// <returns>A task representing the asynchronous operation.</returns>\n    /// <exception cref=\"InvalidOperationException\">Thrown if the runtime is already started.</exception>\n    public Task StartAsync(CancellationToken cancellationToken = default)\n    {\n        if (this._shutdownSource != null)\n        {\n            throw new InvalidOperationException(\"Runtime is already running.\");\n        }\n\n        this._shutdownSource = new CancellationTokenSource();\n        this._messageDeliveryTask = Task.Run(() => this.RunAsync(this._shutdownSource.Token), cancellationToken);\n\n        return Task.CompletedTask;\n    }\n\n    /// <summary>\n    /// Stops the runtime service.\n    /// </summary>\n    /// <param name=\"cancellationToken\">Token to propagate when stopping the runtime.</param>\n    /// <returns>A task representing the asynchronous operation.</returns>\n    /// <exception cref=\"InvalidOperationException\">Thrown if the runtime is in the process of stopping.</exception>\n    public Task StopAsync(CancellationToken cancellationToken = default)\n    {\n        if (this._shutdownSource != null)\n        {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Runtime/InProcess/InProcessRuntime.cs#L38-L74","documentation":"InProcessRuntime.StartAsync throws InvalidOperationException if _shutdownSource is already non-null, which denotes the runtime is started. There is no restart path until FinishAsync (triggered by StopAsync/RunUntilIdleAsync) nulls _shutdownSource.","triggerScenarios":"Calling StartAsync twice without stopping/disposing the runtime; reusing one InProcessRuntime across sequential test scenarios; a DI singleton runtime started in multiple places.","commonSituations":"Test harnesses that start the runtime per test but share an instance; background services that start the runtime on each trigger; forgetting DisposeAsync resets state only via RunUntilIdleAsync (it does not null _shutdownSource unless the delivery task finishes).","solutions":["Start the runtime once per lifecycle; call StopAsync/RunUntilIdleAsync or DisposeAsync before restarting.","Create a new InProcessRuntime instance for each independent run.","Guard startup with a flag so repeated StartAsync calls are no-ops."],"exampleFix":"// before\nruntime.StartAsync();\n// ...\nruntime.StartAsync(); // throws\n\n// after\nruntime.StartAsync();\n// ...\nawait runtime.RunUntilIdleAsync();\nruntime.StartAsync(); // ok after idle finishes","handlingStrategy":"validation","validationCode":"bool started = false;\nvoid SafeStart(InProcessRuntime rt)\n{\n    try { rt.StartAsync(); started = true; }\n    catch (InvalidOperationException) { /* already started */ }\n}","typeGuard":null,"tryCatchPattern":"try { runtime.StartAsync(); }\ncatch (InvalidOperationException) { /* already running */ }","preventionTips":["Start each InProcessRuntime exactly once per lifecycle.","Stop/RunUntilIdleAsync or DisposeAsync before restarting.","Use a fresh runtime instance per independent run."],"tags":["inprocess-runtime","lifecycle","startup","runtime","dotnet"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}