Skip to content

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

Closed

Description

@mfeingol

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

Activity

  1. Cristian-Azeta commented on Jun 12, 2025

    @Cristian-Azeta

    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
    }
    
    
  2. e0548965 commented on Jun 13, 2025

    @e0548965

    Having the same issue, any workaround? Quite big issue for us after just completing the upgrade to v12.0.0

  3. AlanHoiland commented on Jun 13, 2025

    @AlanHoiland

    I'm seeing the same issue. Doesn't happen every time, maybe 50%.

  4. crwsolutions commented on Jun 13, 2025

    @crwsolutions
  5. e0548965 commented on Jun 13, 2025

    @e0548965

    Happens consistently for us every time

  6. mfeingol commented on Jun 13, 2025

    @mfeingol
    Author

    Same here - happens every time.

  7. bijington commented on Jun 13, 2025

    @bijington
    Contributor

    Perhaps one of you can provide a sample repository?

  8. TheCodeTraveler commented on Jun 13, 2025

    @TheCodeTraveler
  9. TheCodeTraveler commented on Jun 13, 2025

    @TheCodeTraveler
    Collaborator

    Hi gang!

    1. The bug with the Android Back Button is unrelated to this Issue as reported by @mfeingol
    2. I am unable to reproduce @mfeingol's issue.
    [Fact(Timeout = (int)TestDuration.Short)]
    public async Task ShowPopupAsync_TaskShouldCompleteWhenPopupCloseAsyncIsCalled()
    {
    	// Arrange
    	Task<IPopupResult> showPopupAsyncTask;
    	var popup = new Popup();
    
    	if (Application.Current?.Windows[0].Page is not Page page)
    	{
    		throw new InvalidOperationException("Page cannot be null");
    	}
    
    	// Act
    	showPopupAsyncTask = page.ShowPopupAsync(popup, token: TestContext.Current.CancellationToken);
    
    	// Assert
    	Assert.Single(page.Navigation.ModalStack);
    	Assert.IsType<PopupPage>(page.Navigation.ModalStack[0]);
    
    	// Act
    	await popup.CloseAsync(TestContext.Current.CancellationToken);
    	await showPopupAsyncTask;
    
    	// Assert
    	Assert.Empty(page.Navigation.ModalStack);
    }
  10. added a commit that references this issue on Jun 13, 2025
  11. e0548965 commented on Jun 13, 2025

    @e0548965

    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

  12. mfeingol commented on Jun 16, 2025

    @mfeingol
    Author

    @TheCodeTraveler:

    Please give this a spin: https://github.com/mfeingol/repros/tree/master/CommunityToolkit_2703_repro

    It's not 100%, but if you press the button and raise a popup, then light-dismiss it and don't see an immediate alert displayed, then you're seeing a repro. (Aside: I'm not quite sure why the popup isn't displaying its XAML and is just a little white box, but that's probably another story).

  13. 25 remaining items

  14. stefanbogaard86 commented on Jun 20, 2025

    @stefanbogaard86
    Contributor

    Hello @bijington, the debugger attached seems to make it worse, but it also occurs in release builds.

  15. bijington commented on Jun 20, 2025

    @bijington
    Contributor

    It's taken a lot of attempts but have seen it fail 2 times out of maybe 50 shows/closes. It doesn't feel any closer but let's hope I can find a way to reliably reproduce it

  16. FrederikFBrandt commented on Jun 20, 2025

    @FrederikFBrandt

    In my case it fails 45 out of 50 shows/closes. And even 1 failure means the user flow is interrupted, it has a huge impact. I have only tested on Android

  17. bijington commented on Jun 20, 2025

    @bijington
    Contributor

    I don't think we are questioning the impact but we are questioning our ability to reproduce the issue.

    What development environment are you using?

    The sooner we get to a set of reliably reproducible steps the easier this is going to be to fix.

    Also are you testing against the supplied sample repository or your own application?

  18. mfeingol commented on Jun 20, 2025

    @mfeingol
    Author

    @bijington:

    I'm using Windows 11, VS 2022 [latest], and an Android emulator running Android 15 with 4GB of RAM.

    I played with disabling .NET and XAML hot reload and observed the following:

    1. Disable both hot reload options, hit F5 - no repro
    2. Re-enable both hot-reload options, hit F5 - no repro
    3. Restart VS, hit F5 - easy repro

    I'm not sure I understand the pattern here, but hot reload does seem to be required for the repro timing.

    Question - is there a weak reference in here somewhere that might be causing this if the GC were to run more aggressively?

  19. mfeingol commented on Jun 20, 2025

    @mfeingol
    Author

    @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
  20. bijington commented on Jun 20, 2025

    @bijington
    Contributor

    @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

  21. bijington commented on Jun 20, 2025

    @bijington
    Contributor

    @mfeingol I did find time to have a go and I have a PR #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

  22. mfeingol commented on Jun 20, 2025

    @mfeingol
    Author

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

  23. bijington commented on Jun 20, 2025

    @bijington
    Contributor

    Thank YOU for all the assistance too! It was easy once you discovered that it was garbage collection related

  24. CyberZeus commented on Jun 20, 2025

    @CyberZeus

    Did you plain to make a release that includes this fix?
    Thanks for your work!

  25. bijington commented on Jun 20, 2025

    @bijington
    Contributor

    Yep I think we might have another fix or two to look into and then we can get a release out

  26. locked and limited conversation to collaborators on Jun 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions