Maui: [BUG] Popup V2 - Android - ShowPopupAsync(INavigation) returns a Task that never completes

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the “Reporting a bug” section on Contributing file?

Current Behavior

When I call ShowPopupAsync and then call CloseAsync() from inside my popup, the popup closes but ShowPopupAsync’s task never completes so the result is never available.

Expected Behavior

I expect the task to complete when I close the popup.

Steps To Reproduce

  1. Derive a popup from Popup<bool?>
  2. Call OpenPopupAsync() on it, and close the popup from within using CloseAsync()
  3. Note that await OpenPopupAsync() never returns

Link to public reproduction project repository

https://github.com/mfeingol/repros/tree/master/CommunityToolkit_2703_repro

Environment

- .NET MAUI CommunityToolkit: 12.0.0
- OS: Windows 11
- .NET MAUI: 9.0.71

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 8
  • Comments: 47 (19 by maintainers)

Commits related to this issue

Most upvoted comments

@bijington I gave the PR bits a quick spin and they appear to have resolved the problem. Thanks for the legwork here.

@mfeingol I did find time to have a go and I have a PR https://github.com/CommunityToolkit/Maui/pull/2756 that should fix it.

If you get a chance before the PR merges then you could try testing the fix by following the guide at https://github.com/CommunityToolkit/Maui/wiki/Preview-Packages

@mfeingol excellent work! I can confirm that I can reproduce 100% of the time now.

I’ve had a Quick Look at our code and there is in fact a WeakEventManager instance for handling the popup close events. I might not be able to look at it today but it looks like we might need a stronger reference.

I think the first step will be to reproduce this in a unit test (which I hope should be straightforward given your investigation work) and then debug/fix it

@bijington:

OK, I think I have a winner. I added GC.Collect() calls before and after the call to CloseAsync() and I now have a 100% repro with and without hot reload and even without a debugger.

  1. Pull my repro above to the latest
  2. Run the MCT 2703 repro
  3. Hit the close button in the popup ----> this is key; light-dismissing the popup generally works fine

My point was just to highlight that the bug involved swallowing touch input which may have prevented your tapped event from firing.

@bijington: thanks for looking. I accepted your PR and made a couple of tweaks to the project to get it to build successfully on Android. The popup now renders in its full garish color glory. 😃

Now… without a debugger the sample does not reproduce. However, with a debugger attached to an Android emulator I’m able to reproduce the issue very reliably. I’ve attached a video of me clicking on the popup close button three times - in two cases the repro occurs.

https://github.com/user-attachments/assets/0c6d6ca4-eeee-4ac2-975f-4693f7b57a67

@mfeingol I managed to work out the rendering issues - I have opened a PR to your repros repository to fix this. It looks like you had some bugs in your repro https://github.com/mfeingol/repros/pull/1

As for the main topic of this bug I am still unable to reproduce the issue that you mention even with the debugger attached.

@mfeingol thanks for providing the sample. I see that the sample isn’t using the ShowPopupAsync(INavigation) method as the title suggests, is this intentional or does it highlight that it isn’t just the specific method you as having the issue with?

It seems to repro either way. Using the default shell mechanism was faster and I didn’t have a lot of time.

The problem seems to be in the TaskCompletionSource of the ShowPopupAsync method in the PopupExtensions class. If you bring the TaskCompletionSource inside the PopupPage class the close event is called. The Subscription object of the WeakEventManager class has the Target null when the Close of the popup is executed.

Thanks for looking into this @TheCodeTraveler , can you edit your test to actually return a DTO/value from the Popup? I’ve tried a lot different things but I just cannot get the task to return.

I think this is the same issue: https://developercommunity.visualstudio.com/t/Popup-v2-in-CommunityToolkitMaui-1200/10921966?viewtype=all&space=21&ftype=problem&sort=newest

I’m seeing the same issue. But only when I use the system back button (< ) (On android, haven’t tried iOs).

Example in Android …

TestPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:tk="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="TEST.TestPage"
             Title="TEST">

	<Label Text="TEST" />

</ContentPage>

TestPage.xaml.cs

namespace TEST;

using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Mvvm.Messaging;

public partial class TestPage : ContentPage
{
    #region Constructor

    public TestPage()
    {
        InitializeComponent();
    }

    #endregion

    #region Event

    protected override Boolean OnBackButtonPressed()
    {
        return true;
    }

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        IsBusy = true;
        try
        {
		IPopupResult<Boolean> result = await this.ShowPopupAsync<Boolean>(new TestPopup(), new PopupOptions
		{
			CanBeDismissedByTappingOutsideOfPopup = false,
			PageOverlayColor = Colors.Orange.WithAlpha(0.4f),
		}, CancellationToken.None);
		if (result.Result is true) // <<<<<<<<<<<<<<<<<<<<<<<<<<< NEVER GO HERE
		{
			// Yes was tapped
		}
		else
		{
			// No was tapped
		}
        }
        catch (Exception ex)
        {
            Vibration.Default.Vibrate(1000);
            await DisplayAlert(Title, ex.Message, "Continue");
        }
        finally
        {
            IsBusy = false;
        }
    }

    #endregion
}

TestPopup.xaml

<?xml version="1.0" encoding="utf-8" ?>
<tk:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:tk="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
          x:TypeArguments="x:Boolean"
          x:Class="TEST.TestPopup"
          HorizontalOptions="Fill"
          VerticalOptions="Fill"
          BackgroundColor="Red">

	<Label Text="TEST" />
	<Button Text="Close" Clicked="OnCancelClicked" />

</tk:Popup>

TestPopup.xaml.cs

namespace TEST;

using CommunityToolkit.Maui.Views;

public partial class TestPopup : Popup<Boolean>
{
    #region Constructor

    public TestPopup()
    {
        InitializeComponent();
        WidthRequest = 0.8 * (DeviceDisplay.Current.MainDisplayInfo.Width / DeviceDisplay.Current.MainDisplayInfo.Density);
        HeightRequest = 0.8 * (DeviceDisplay.Current.MainDisplayInfo.Height / DeviceDisplay.Current.MainDisplayInfo.Density);
    }

    #endregion

    #region Event

    private async void OnCancelClicked(Object sender, EventArgs e)
    {
        try
        {
	    await CloseAsync(false, CancellationToken.None);
        }
        catch { }
    }

    #endregion
}