Skip to content

Commit df5cae6

Browse files
committed
Fix TryObtainLockAsync(TimeSpan.Zero) overload
In the case of a zero timeout, the underlying async lock would be left in an inconsistent state causing a subsequent attempt to obtain the lock to deadlock.
1 parent 0649d71 commit df5cae6

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

AsyncLock/AsyncLock.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ internal async Task<IDisposable> ObtainLockAsync(CancellationToken ct = default)
8888
{
8989
if (await TryEnterAsync(timeout))
9090
{
91+
// Reset the owning thread id after all await calls have finished, otherwise we
92+
// could be resumed on a different thread and set an incorrect value.
93+
_parent._owningThreadId = ThreadId;
94+
// In case of !synchronous and success, TryEnter() does not release the reentrancy lock
95+
_parent._reentrancy.Release();
9196
return this;
9297
}
9398
return null;

0 commit comments

Comments
 (0)