Skip to content

Commit 004d68b

Browse files
authored
Merge pull request #1426 from microsoft/dev/lifengl/fixAsyncLazyRaceCondition
Fix a race condition which leads product to hang
2 parents 56c02ec + 46b079d commit 004d68b

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/Microsoft.VisualStudio.Threading/AsyncLazy`1.cs

+7
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ public Task<T> GetValueAsync(CancellationToken cancellationToken)
224224
// to synchronously block the Main thread waiting for the result
225225
// without leading to deadlocks.
226226
this.joinableTask = this.jobFactory.RunAsync(valueFactory);
227+
228+
// this ensures that this.joinableTask must be committed before this.value
229+
Thread.MemoryBarrier();
230+
227231
this.value = this.joinableTask.Task;
228232
}
229233
else
@@ -245,6 +249,9 @@ public Task<T> GetValueAsync(CancellationToken cancellationToken)
245249
resumableAwaiter?.Resume();
246250
}
247251

252+
// this ensures that this.joinableTask cannot be retrieved before the conditional check using this.value
253+
Thread.MemoryBarrier();
254+
248255
return this.joinableTask?.JoinAsync(continueOnCapturedContext: false, cancellationToken) ?? this.value.WithCancellation(cancellationToken);
249256
}
250257

0 commit comments

Comments
 (0)