Skip to content

Commit 074b033

Browse files
authored
Merge pull request #1505 from manofstick/manofstick-threading-concerns
Race Conditions in FSharp.Core
2 parents 359df98 + 1d685c3 commit 074b033

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/fsharp/FSharp.Core/quotations.fs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,16 @@ open Helpers
9191
[<CompiledName("FSharpVar")>]
9292
[<System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage","CA2218:OverrideGetHashCodeOnOverridingEquals",Justification="Equals override does not equate further objects, so default GetHashCode is still valid")>]
9393
type Var(name: string, typ:Type, ?isMutable: bool) =
94-
9594
inherit obj()
96-
static let mutable lastStamp = 0L
95+
96+
static let getStamp =
97+
let mutable lastStamp = -1L // first value retrieved will be 0
98+
fun () -> System.Threading.Interlocked.Increment &lastStamp
99+
97100
static let globals = new Dictionary<(string*Type),Var>(11)
98101

99-
let stamp = lastStamp
102+
let stamp = getStamp ()
100103
let isMutable = defaultArg isMutable false
101-
do lock globals (fun () -> lastStamp <- lastStamp + 1L)
102104

103105
member v.Name = name
104106
member v.IsMutable = isMutable

src/fsharp/FSharp.Core/reflect.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,10 @@ module internal Impl =
404404
| _ -> invalidArg "tys" (SR.GetString(SR.invalidTupleTypes))
405405

406406
let tables = if isStruct then valueTupleTypes else refTupleTypes
407-
match tables.TryGetValue(asm) with
407+
match lock dictionaryLock (fun () -> tables.TryGetValue(asm)) with
408408
| false, _ ->
409+
// the Dictionary<>s here could be ConcurrentDictionary<>'s, but then
410+
// that would lock while initializing the Type array (maybe not an issue)
409411
let a = ref (Array.init<Type> 8 (fun i -> makeIt (i + 1)))
410412
lock dictionaryLock (fun () -> match tables.TryGetValue(asm) with
411413
| true, t -> a := t

0 commit comments

Comments
 (0)