Protect varTable and assemblyTable with typeTablesLock for thread safety (#525) - #526
Merged
Merged
Conversation
The var translation tables (varTableFwd/varTableBwd) and assemblyTableFwd have the same check-then-create race as the type tables fixed in #522. Under ParallelCompilation, concurrent quotation translation or assembly resolution can hit the unguarded Dictionary with duplicate Add calls (ArgumentException) or produce duplicate target objects. Wrap convVarToSrc, convVarToTgt, and convProvidedAssembly in the existing typeTablesLock. The lock is already re-entrant (Monitor), so the recursive calls to convTypeToTgt/convTypeToSrc inside these functions are safe. Fixes #525
Three new tests exercise the translation tables under parallel access: 1. Concurrent ConvertSourceExprToTarget on shared Vars - exercises convVarToTgt/convVarToSrc under contention (8 threads, shared Var instances, barrier-synchronized start). 2. Concurrent ConvertSourceTypeToTarget - exercises convType/convTypeRef with various type kinds (generics, arrays, primitives) from 8 threads. 3. Concurrent type translation through ProvidedAssembly - exercises convProvidedAssembly by translating 16 types sharing one ProvidedAssembly from 8 threads simultaneously. Without the locks from #522 and this PR, these tests would fail with ArgumentException (duplicate Dictionary key) or state corruption.
Member
|
Hmm, the new version of ProvidedTypes.fs seems to be slow in a real environment. (At least it works, unlike the old version, so that's good.) Could the locking also be making it slow? I tested #522 alone, and it felt ok. Do we have any (parallel) perf tests to have any rough estimates of figures? |
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.
Problem
varTableFwd/varTableBwd and assemblyTableFwd in ProvidedTypesContext have the same unguarded check-then-create race condition that typeTableFwd/typeTableBwd had before #522.
Under ParallelCompilation=true (default in recent .NET SDKs):
Fix
Wrap convVarToSrc, convVarToTgt, and convProvidedAssembly in the existing typeTablesLock (introduced in #522). The lock is already re-entrant (Monitor), so the recursive calls to convTypeToTgt/convTypeToSrc inside these functions are safe.
The lock comment is updated to reflect the broader scope.
Validation
Fixes #525