AsyncLock icon indicating copy to clipboard operation
AsyncLock copied to clipboard

AsyncLock is not 'await using' (IAsyncDisposable) compatible

Open 0xF6 opened this issue 1 year ago • 2 comments

image

Yes, of course, i known what can i do using(await lock.AsyncLock()), but this is not entirely intuitive given the official syntax for IAsyncDisposable

If you don't mind, i can create a PR~

0xF6 avatar Oct 04 '22 14:10 0xF6

Hi @0xF6,

As you know, these are different semantics. await using is to call IAsyncDisposable.DisposeAsync asynchronously, but using (await foo.LockAsync()) acquires the lock asynchronously. It's also perfectly plausible to mix both (await using (await foo.LockAsync())) to both acquire and dispose asynchronously.

If you're in an async context, you need to use await foo.LockAsync() (regardless of whether you do using (await foo.LockAsync()) or you do await using (await foo.LockAsync())) so that the event loop isn't blocked while the lock is acquired.

I'm not against implementing IAsyncDisposable but there's no real need for it since there's no IO taking place during cleanup and no benefit over calling IDisposable.Dispose() synchronously. We also want to make sure that invalid code (not invoking await on LockAsync()) doesn't accidentally compile.

Feel free to open a PR (with tests!) if you want to try that, but like I said, there are some issues that need to be kept in mind.

mqudsi avatar Oct 04 '22 17:10 mqudsi

Feel free to open a PR (with tests!)

Oke, i'll do it on weekend~

0xF6 avatar Oct 06 '22 04:10 0xF6