Skip to content

Investigate the CheckAccess pattern in Blazor #12092

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

Closed
rynowak opened this issue Jul 11, 2019 · 1 comment
Closed

Investigate the CheckAccess pattern in Blazor #12092

rynowak opened this issue Jul 11, 2019 · 1 comment
Assignees
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@rynowak
Copy link
Member

rynowak commented Jul 11, 2019

See: #11930 (comment)

I suspect that this test isn't implemented correctly, and as a result it's possible to run work concurrently on the sync context.

@rynowak rynowak added area-blazor Includes: Blazor, Razor Components investigate labels Jul 11, 2019
@mkArtakMSFT mkArtakMSFT added this to the 3.0.0-preview9 milestone Jul 12, 2019
@rynowak
Copy link
Member Author

rynowak commented Aug 1, 2019

I don't think there's an issue here. I tested with the following code:

@code  { 

    protected override void OnInitialized()
    {
        Task.Run(() =>
        {
            Console.WriteLine($"SynchronizationContext.Current: {System.Threading.SynchronizationContext.Current}");

            _ = InvokeAsync(() =>
            {
                Console.WriteLine($"SynchronizationContext.Current: {System.Threading.SynchronizationContext.Current}");
                System.Threading.Interlocked.Increment(ref counter);
                for (var i = 0; i < 10; i++)
                {
                    Console.WriteLine($"A: {counter}");
                }
                System.Threading.Interlocked.Decrement(ref counter);
                Console.WriteLine($"SynchronizationContext.Current: {System.Threading.SynchronizationContext.Current}");
            });
        });

        System.Threading.Interlocked.Increment(ref counter);
        for (var i = 0; i < 10; i++)
        {
            Console.WriteLine($"A: {counter}");
        }
        System.Threading.Interlocked.Decrement(ref counter);
    }

    int counter = 0;
}

If the counter ever showed two, this would indicate a bug.

When you start a new task via Task.Run it won't inherit the sync context. Therefore when you call InvokeAsync inside it, it will do a proper dispatch.

So there is one way you could break our invariant, which would be to set the your current sync context to ours, and then call InvokeAsync. This doesn't seem like something that will happen in valid/reasonable code.

@rynowak rynowak closed this as completed Aug 1, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

2 participants