microsoft/autogen · error · InvalidOperationException
Error initializing: {this.GetType().FullName}; already initi
Error message
Error initializing: {this.GetType().FullName}; already initialized. What it means
RunContext layers enforce initialize-once semantics with Interlocked.CompareExchange on an 'initialized' flag. PrepareInitialize calls OnInitializeError when the context is already initialized (flag == 1), and the default implementation throws InvalidOperationException naming the type: initializing the same RunContext layer twice is a state-machine violation. (The mirror message '...not initialized' fires for double-deinitialize.)
Source
Thrown at dotnet/src/Microsoft.AutoGen/AgentChat/GroupChat/RunContext.cs:31
private void PrepareInitialize(Action errorAction)
{
if (Interlocked.CompareExchange(ref this.initialized, 1, 0) != 0)
{
errorAction();
}
}
private void PrepareDeinitialize(Action errorAction)
{
if (Interlocked.CompareExchange(ref this.initialized, 0, 1) != 1)
{
errorAction();
}
}
protected bool IsInitialized => Volatile.Read(ref this.initialized) == 1;
protected virtual void OnInitializeError() => throw new InvalidOperationException($"Error initializing: {this.GetType().FullName}; already initialized.");
protected virtual void OnDeinitializeError() => throw new InvalidOperationException($"Error deinitializing: {this.GetType().FullName}; not initialized.");
public ValueTask InitializeAsync()
{
this.PrepareInitialize(this.OnInitializeError);
return this.InitializeCore();
}
public ValueTask DeinitializeAsync()
{
this.PrepareDeinitialize(this.OnDeinitializeError);
return this.DeinitializeCore();
}
protected abstract ValueTask InitializeCore();
protected abstract ValueTask DeinitializeCore();
}
View on GitHub (pinned to 027ecf0a37)
Solutions
- Deinitialize (or fully reset) the context before initializing it again; do not reuse one team instance across concurrent runs
- Serialize runs against the same instance, or create a fresh team per run/conversation
- In custom RunContext subclasses, treat OnInitializeError as the override point if you want idempotent initialization instead of a throw
Example fix
// before await team.RunAsync(t1); // initializes await team.RunAsync(t2); // concurrent or without reset -> double init // after using var team1 = BuildTeam(); await team1.RunAsync(t1); using var team2 = BuildTeam(); await team2.RunAsync(t2); // fresh instance per run
Defensive patterns
Strategy: validation
Validate before calling
// Prevent concurrency at the call site: one run per team instance at a time
if (teamRunInProgress) throw new InvalidOperationException("Team is already running; await it or reset before re-running");
Interlocked.Increment(ref teamRunInProgress);
try { await team.RunAsync(task, ct); } finally { Interlocked.Decrement(ref teamRunInProgress); } Try / catch
catch (InvalidOperationException ex) when (ex.Message.Contains("already initialized"))
{
// deinitialize/reset the context (or recreate the team) and retry once
} Prevention
- Never run one team instance concurrently; create a fresh team per run or conversation
- Deinitialize/reset before re-initializing any RunContext-based layer
- In custom layers, override OnInitializeError for idempotent behavior if double-init should be a no-op
When it happens
Trigger: Calling InitializeAsync twice on the same RunContext layer without an intervening DeinitializeAsync — e.g. two concurrent RunAsync calls on a group chat sharing one runtime layer, or manual initialization followed by a run that initializes again.
Common situations: Concurrent/parallel runs of the same team instance; retry logic that re-runs initialization after a partial failure without deinitializing; custom layers that call InitializeCore's base at the wrong point.
Related errors
- The group chat has not been initialized. It must be run befo
- The recent update is not a TextMessage
- Kernel is not running
- Installing directory is not set
- The content has more than one choice. Please try another inp
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/30f19a826d4381e9.
Report an issue: GitHub.