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

Ergonomics could be better #1

Open
matthew-a-thomas opened this issue Jul 28, 2022 · 0 comments
Open

Ergonomics could be better #1

matthew-a-thomas opened this issue Jul 28, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@matthew-a-thomas
Copy link
Owner

matthew-a-thomas commented Jul 28, 2022

As of 0.1.9, guarded side effects are allowed by any await within the guarded section (if the lock has been reentered), not just by the await on the task that reentered the lock.

For example, this program throws an exception when the casual observer might expect it to work:

namespace Example;

using System.Threading.Tasks;
using ReentrantAsyncLock;
using Xunit;

class Program
{
    static async Task Main()
    {
	var gate = new ReentrantAsyncLock();
	var state = 0;
	async Task ChangeGuardedStateAsync()
	{
	    await using (await gate.LockAsync(default))
            {
                state++;
            }
        }
        await using (await gate.LockAsync(default))
        {
            var task = ChangeGuardedStateAsync();
            var stateBefore = state;
            await Task.Yield(); // Seemingly unrelated
            Assert.Equal(stateBefore, state); // ...but it allows side effects on the guarded state (this line throws)
            await task;
        }
    }
}

.NET Fiddle: https://dotnetfiddle.net/FzQ1qR

Thanks to Max Fedotov for patiently explaining this to me.

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

No branches or pull requests

1 participant