Skip to content

WaitHandles: Possible race condition in InternalWaitOneAsync() #120

Closed

Description

@madelson

Referring to https://github.com/madelson/DistributedLock/blob/master/DistributedLock.WaitHandles/WaitHandleExtensions.cs#L43

Consider the following case:

  • CancellationToken fires and sets the task to canceled
  • WaitForSingleObject callback runs and fails to set the task

In that case, I think we lose a signal on the handle; ideally if the wait callback fails to set the task then it should re-signal the event.

Activity

  1. added this to the 2.3.1 milestone on Jul 6, 2022
  2. madelson commented on Jul 8, 2022

    @madelson
    OwnerAuthor

    Reproduction:

    [Test]
            public async Task TestCancellationDoesNotLeadToLostSignal([Values] bool async)
            {
                var semaphore = new WaitHandleDistributedSemaphore(nameof(this.TestCancellationDoesNotLeadToLostSignal), 2);
                await using var _ = await semaphore.AcquireAsync(TimeSpan.FromSeconds(1));
    
                for (var i = 0; i < 50; ++i)
                {
                    using var barrier = new Barrier(2);
                    using var source = new CancellationTokenSource();
                    var acquireTask = Task.Run(async () =>
                    {
                        barrier.SignalAndWait();
                        try
                        { 
                            if (async) { await using var _ = await semaphore.AcquireAsync(cancellationToken: source.Token); }
                            else { using var _ = semaphore.Acquire(cancellationToken: source.Token); }
                        }
                        catch when (source.Token.IsCancellationRequested) { }
                    });
                    var cancelTask = Task.Run(() =>
                    {
                        barrier.SignalAndWait();
                        source.Cancel();
                    });
                    await Task.WhenAll(acquireTask, cancelTask);
                }
    
                await using var handle = await semaphore.TryAcquireAsync();
                Assert.IsNotNull(handle); // if we lost even a single signal due to cancellation in the loop above, this will fail
            }
  3. added 2 commits that reference this issue on Jul 8, 2022
    4c42a79
    f4a3401
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions