{"record":{"id":"30f19a826d4381e9","repo":"microsoft/autogen","slug":"error-initializing-this-gettype-fullname-alr","errorCode":null,"errorMessage":"Error initializing: {this.GetType().FullName}; already initialized.","messagePattern":"Error initializing: (.+?); already initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/AgentChat/GroupChat/RunContext.cs","lineNumber":31,"sourceCode":"    private void PrepareInitialize(Action errorAction)\n    {\n        if (Interlocked.CompareExchange(ref this.initialized, 1, 0) != 0)\n        {\n            errorAction();\n        }\n    }\n\n    private void PrepareDeinitialize(Action errorAction)\n    {\n        if (Interlocked.CompareExchange(ref this.initialized, 0, 1) != 1)\n        {\n            errorAction();\n        }\n    }\n\n    protected bool IsInitialized => Volatile.Read(ref this.initialized) == 1;\n\n    protected virtual void OnInitializeError() => throw new InvalidOperationException($\"Error initializing: {this.GetType().FullName}; already initialized.\");\n    protected virtual void OnDeinitializeError() => throw new InvalidOperationException($\"Error deinitializing: {this.GetType().FullName}; not initialized.\");\n\n    public ValueTask InitializeAsync()\n    {\n        this.PrepareInitialize(this.OnInitializeError);\n        return this.InitializeCore();\n    }\n\n    public ValueTask DeinitializeAsync()\n    {\n        this.PrepareDeinitialize(this.OnDeinitializeError);\n        return this.DeinitializeCore();\n    }\n\n    protected abstract ValueTask InitializeCore();\n    protected abstract ValueTask DeinitializeCore();\n}\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/AgentChat/GroupChat/RunContext.cs#L13-L49","documentation":"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.)","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nawait team.RunAsync(t1); // initializes\nawait team.RunAsync(t2); // concurrent or without reset -> double init\n// after\nusing var team1 = BuildTeam(); await team1.RunAsync(t1);\nusing var team2 = BuildTeam(); await team2.RunAsync(t2); // fresh instance per run","handlingStrategy":"validation","validationCode":"// Prevent concurrency at the call site: one run per team instance at a time\nif (teamRunInProgress) throw new InvalidOperationException(\"Team is already running; await it or reset before re-running\");\nInterlocked.Increment(ref teamRunInProgress);\ntry { await team.RunAsync(task, ct); } finally { Interlocked.Decrement(ref teamRunInProgress); }","typeGuard":null,"tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message.Contains(\"already initialized\"))\n{\n    // deinitialize/reset the context (or recreate the team) and retry once\n}","preventionTips":["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"],"tags":["autogen","lifecycle","run-context","double-initialize","thread-safety","csharp"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}