Fix data races that corrupted concurrent conversions - #160
Merged
Conversation
TypeNameParser was a static class holding its entire recursive-descent parse state in static fields, including a shared StringBuilder. Two conversions running concurrently interleaved their Clear()/Append() calls and spliced each other's output, producing results like "public static voidvoid Main(string[] args)". Concurrent mutation of the shared token array and index could also throw IndexOutOfRangeException outright. Make TypeNameParser an instance class, constructed fresh per ParseTypeName call, so no parse state is shared between threads. The public entry point is unchanged. Also make TypeHelper._typeNameConversions a ConcurrentDictionary. It is mutated during conversion by ClassOrInterfaceDeclarationVisitor (to register interface renames when StartInterfaceNamesWithI is set) while other threads read it in ConvertType, which is undefined behavior for Dictionary<,>. While converting, drop two now-dead defensive checks: _tokens is always assigned in the constructor so its null check is unnecessary, and _translate is required, so _translate?.Invoke(...) was silently appending an empty string instead of failing. Add ConcurrencyTests covering both parallel ConvertType calls and parallel end-to-end conversions. Both fail reliably against the old code and pass with the fix. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the intermittent test failures noted in #159.
JavaToCSharpwas not safe to call concurrently. This affects real consumers converting files in parallel, and it was also causing flaky CI: roughly 1 run in 10 failed, in a different test class each time.The bug
TypeNameParserwas astaticclass holding its entire recursive-descent parse state in static fields — including a sharedStringBuilder:ConvertTypeis on the hot path of essentially every conversion, and xUnit runs test classes in parallel. Two conversions interleaving_sb.Clear()/_sb.Append()splice each other's output — the symptom that led me here was generated code containing:The return type emitted twice. Concurrent mutation of the shared token array and index can also throw
IndexOutOfRangeExceptionoutright, which is what the new test hits first against the old code.The fix
TypeNameParseris now an instance class, constructed fresh perParseTypeNamecall, so no parse state is shared. The public entry point is unchanged — this is not a breaking change.TypeHelper._typeNameConversionsis now aConcurrentDictionary. It's mutated during conversion byClassOrInterfaceDeclarationVisitor(registering interface renames whenStartInterfaceNamesWithIis set) while other threads read it inConvertType— undefined behavior forDictionary<,>._tokensis always assigned in the constructor, and_translate?.Invoke(...)was silently appending an empty string on null rather than failing.Testing
New
ConcurrencyTestscovers parallelConvertTypecalls and parallel end-to-end conversions. I verified these are genuine regression tests: both fail reliably against the old code (stashed the fix and confirmed) and pass with it.Full suite run 15 consecutive times: 286/286 passing every time. On
masterthe same loop reproduces a failure within ~10 runs.Note on scope
TypeHelper._typeNameConversionsbeing process-global mutable state is still a design smell — per-file class renames leak between conversions, so converting two files withStartInterfaceNamesWithIcan bleed one file's renames into the other. The proper fix is moving it ontoConversionContext, butConvertTypehas ~60 call sites and is public API used directly by tests, so that's a larger breaking change. This PR makes it safe without changing the API; happy to do the deeper refactor separately if you want it.🤖 Generated with Claude Code