You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Concurrent Generate calls throw or return wrong code: AddLanguageGenerators registers generators as singletons that keep per-call state in fields #285
ServiceCollectionExtensions.AddLanguageGenerators registers every generator, and the LanguageGeneratorResolver that holds them, as a singleton (Coder/ServiceCollectionExtensions.cs:26-35). Generate keeps its working state in instance fields on that shared object:
StandardLanguageGenerator: staticTypeScopes, a Stack (Coder/Languages/StandardLanguageGenerator.cs:31)
per-generator fields such as insideType, constructedType, bodyDepth, insideInterface, writtenDownParameters, instanceInitialisedFields and implBounds (GoGenerator.cs:180-205, CGenerator.cs:117,702, CppGenerator.cs:43, PythonGenerator.cs:23, RustGenerator.cs:95-131)
Nothing documents the generators as single-threaded.
Repro. One instance of each generator, eight different SourceFiles each with a class of 20 methods, and 2000 Generate calls through Parallel.For on 4 cores. Each result was compared with serial output:
Generator
Wrong output
Threw
python
13
1985
javascript
562
1423
cpp
2
1993
c
11
1987
go
6
1994
csharp, rust
0
0 (this run)
The exceptions were NullReferenceException, InvalidOperationException: Stack empty. and InvalidOperationException: Collection was modified….
Why it matters
AddCoder() is the documented way to consume the library, and it gives every host a singleton. A web API, or a build tool generating several files in parallel, will hit this.
Two requests generating at the same time either fail, or quietly return code shaped by the other request's state. For example, DeclaredReturnType (#273) looks callees up in whichever GenerationRoot was set last.
Suggested fix / acceptance criteria
Pick one:
(a) Move per-call state into a context object created inside Generate, so generator instances become stateless.
(b) As a minimal fix, serialise Generate with a per-instance lock and document that.
(c) Register generators as transient and have LanguageGeneratorResolver create a fresh instance per lookup.
Add a test that runs Generate concurrently on one instance of each generator over several different documents. It should assert that every result equals the serial output and that nothing throws.
Priority: High. AddCoder() is the documented way to consume the library, and it hands every host stateful singletons. Concurrent callers get exceptions, and some get wrong code with no error (562/2000 for JavaScript in the repro). Any web API or parallel build tool will hit this.
Area / suggested owner: Core generator infrastructure (ServiceCollectionExtensions, LanguageGeneratorBase, StandardLanguageGenerator, plus per-language state fields)
Note: Option (c), transient registration, is the cheapest fix that is correct. Option (a), a per-call context object, is the long-term fix. Option (b), a lock, only papers over the problem.
Activity
matt-edmondson commentedon Oct 19, 2026
Triage
AddCoder()is the documented way to consume the library, and it hands every host stateful singletons. Concurrent callers get exceptions, and some get wrong code with no error (562/2000 for JavaScript in the repro). Any web API or parallel build tool will hit this.ServiceCollectionExtensions,LanguageGeneratorBase,StandardLanguageGenerator, plus per-language state fields)DeclaredReturnTypereading the last-setGenerationRoot) is one visible symptom of the same shared state.Generated by Claude Code