dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.07k stars 1.73k forks source link

Freeze when invoking SecureStorage.GetAsync().Result multiple times on Windows #24687

Closed suugbut closed 1 month ago

suugbut commented 1 month ago

Description

This issue has been fixed in Android: https://github.com/dotnet/maui/pull/17928, but still exists in Windows.

Steps to Reproduce

Replace the code behind of MainPage provided by Maui project's default template with the following code.

public partial class MainPage : ContentPage
{
    int count = 0;

    public MainPage()
    {
        InitializeComponent();
    }

    private async void OnCounterClicked(object sender, EventArgs e)
    {
        if (await SecureStorage.GetAsync("key") is null)
        {
            await SecureStorage.SetAsync("key", "value");
            Title = "key is created";
        }
        else
        {
            count++;

            // async version works for multiple calls
            // var key = await SecureStorage.GetAsync("key");

            // sync version freezes for the second call.
            var key = SecureStorage.GetAsync("key").Result;

            Title = $"{key} - {count}";
        }
    }
}

Affected platforms

Windows

Did you find any workaround?

// but sync version can be fixed with Task.Run()
var key = Task.Run(() => SecureStorage.GetAsync("key")).Result;
github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

mattleibow commented 1 month ago

You really should not just call Result on Tasks as this will most certainly cause deadlocks. You can maybe try GetAsync("X").GetAwaiter().GetResult() but who knows what that will do.

We do not start task or anything in our code, so it is most likely the Windows implementation that is being deadlocked as you try and force it to run on your thread while it is busy doing something.