Skip to content

Protect varTable and assemblyTable with typeTablesLock for thread safety (#525) - #526

Merged
dsyme merged 2 commits into
masterfrom
fix-var-assembly-table-thread-safety
Jul 29, 2026
Merged

Protect varTable and assemblyTable with typeTablesLock for thread safety (#525)#526
dsyme merged 2 commits into
masterfrom
fix-var-assembly-table-thread-safety

Conversation

@dsyme

@dsyme dsyme commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

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):

  • Var tables: Two threads realizing different target types concurrently can both translate quotations referencing the same Var, racing on Dictionary.Add (ArgumentException on duplicate key) or corrupting state via concurrent read/write.
  • Assembly table: The lazy namespace closure in convProvidedTypeDefToTgt can call convProvidedAssembly from multiple threads, duplicating the assembly translation.

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

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.
@dsyme
dsyme merged commit 9e9b7e0 into master Jul 29, 2026
2 checks passed
@Thorium

Thorium commented Jul 29, 2026

Copy link
Copy Markdown
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?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

varTableFwd/varTableBwd and assemblyTableFwd have same thread-safety race as typeTableFwd

2 participants