Skip to content

Concurrent Generate calls throw or return wrong code: AddLanguageGenerators registers generators as singletons that keep per-call state in fields #285

Open

Description

@matt-edmondson

What's wrong

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:

  • LanguageGeneratorBase: GenerationRoot, emittedPreambleItems, preambleCloneOrigins (Coder/Languages/LanguageGeneratorBase.cs:28-40, 76-110)
  • 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.

Activity

  1. added
    bug
    on Oct 19, 2026
  2. matt-edmondson commented on Oct 19, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • 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)
    • Duplicates / in progress: None found. No open PR addresses it. Python and JavaScript use true division on a call result: twice(a) / 4 gives -3.5 where the other targets give -3 #273 (DeclaredReturnType reading the last-set GenerationRoot) is one visible symptom of the same shared state.
    • 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.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions