You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Synchronization in ManualResetValueTaskSourceCore is inconsistent with the memory model #68623
In this implementation, SetResult uses Interlocked.CompareExchange to release the result, but a GetStatus + GetResult, which is typically run concurrently with SetResult, has no synchronization in it, it's just plain non-volatile loads. So an uninitialized or torn _result could be loaded before _continuation and _completed are loaded and observed to be non-null and true respectively.
There also seems to be a similar issue in the case when SetResult is racing against OnCompleted. OnCompleted uses Interlocked.CompareExchange to release _continuation and _continuationState, but if SignalCompletion on line 216 observes _continuation as non-null, it skips the barrier and proceeds to use both _continuation and _continuationState without any synchronization, meaning an uninitialized _continuationState could have been loaded earlier.
Since .NET Core's memory model is not documented (#63474), it's possible it has a property that makes this code correct. In that case please consider this a request to clarify what that property is. My current understanding is that even in the strictest viable memory models, non-volatile loads can be reordered freely.
@mangod9, @stephentoub Can you please at least clarify if this would be a threading bug, correct code that relies on an undocumented feature of the memory model, or something else?
Activity
nagya commentedon Jul 14, 2022
@mangod9, @stephentoub Can you please at least clarify if this would be a threading bug, correct code that relies on an undocumented feature of the memory model, or something else?
stephentoub commentedon Jul 15, 2022
Technically, some of the reads on _continuation should probably be volatile.