Skip to content

Commit 8e3e42b

Browse files
committed
race condition: access to lastStamp outside of lock
lastStamp was assigned to stamp outside the lock, which could have meant that stamp was assigned the same value if multiple threads were creating Var objects simultaneously. Moved it into a function to ensure access was limited to Interlocked function.
1 parent 641bb41 commit 8e3e42b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
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 = -1 // 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

0 commit comments

Comments
 (0)