Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deadlock with AsyncReaderWriterLock #255

Open
blair-ahlquist opened this issue May 16, 2022 · 2 comments
Open

Deadlock with AsyncReaderWriterLock #255

blair-ahlquist opened this issue May 16, 2022 · 2 comments
Assignees

Comments

@blair-ahlquist
Copy link

The following code deadlocks:

        [Test]
        public async Task NitoMultipleReadThenWriteAsync()
        {
            AsyncReaderWriterLock asyncLock = new AsyncReaderWriterLock();

            void enterReadThenWrite()
            {
                using (asyncLock.ReaderLock()) { }
                using (asyncLock.WriterLock()) { }
            }

            await Task.WhenAll(Enumerable.Range(0, 100).Select(x => Task.Run(enterReadThenWrite)));
        }
@blair-ahlquist
Copy link
Author

Hmm... I went back to this after encountering similar issues with code using SemaphoreSlim. The above code eventually completes after over a minute!

@StephenCleary
Copy link
Owner

I haven't had a chance to run this yet, but that is what I suspected.

This isn't a deadlock, but it is the result of thread pool exhaustion. All the AsyncEx primitives (as well as SemaphoreSlim) require a free thread pool thread to unlock, and if all the thread pool threads are busy waiting, this can cause extremely slow progress. The thread pool will (eventually) add another thread, which is then used either to block immediately (enterReadThenWrite), or used to unlock one of the primitives and then blocked immediately.

As such, this is a duplicate of #107, which I am planning to fix eventually, but haven't yet had time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants