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.24k stars 1.76k forks source link

SemaphoreSlim Release hangs on iOS #23952

Open IeuanWalker opened 3 months ago

IeuanWalker commented 3 months ago

Description

Tryng to prevent a method from being ran multiple times by using SemphoreSlim. But when i call .Release the app hangs and never gets passed the release line.

Similar code works fine on android.

Code is pretty straightforward -

public partial class OsAppSettingsFeature
{
    readonly SemaphoreSlim _semaphore = new(1, 1);
    public async partial Task OpenSettings()
    {
        await _semaphore.WaitAsync();

        TaskCompletionSource tcs = new();

        NSObject observer = UIApplication.Notifications.ObserveDidBecomeActive((_, __) => tcs.SetResult());

        try
        {
            await UIApplication.SharedApplication.OpenUrlAsync(new NSUrl("app-settings:"), new UIApplicationOpenUrlOptions());

            await tcs.Task;
        }
        finally
        {
            observer.Dispose();
            _semaphore.Release();
        }
    }
}

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.70 SR7

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

torfluor commented 3 months ago

I have also experienced that semaphores don't work correctly on iOS. Both in my own code and in the CommunityToolkit MediaElement. Unsure if this is related to MAUI or if it's a more general .Net on iOS issue.

jaosnz-rep commented 2 days ago

@IeuanWalker Could you provide us with a sample project so we can investigate it further? Looking forward to your reply!