-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Fix for App Hang When PopModalAsync Is Called Immediately After PushModalAsync with Task.Yield() #32479
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
Merged
jfversluis
merged 7 commits into
dotnet:inflight/current
from
BagavathiPerumal:fix-32310
Dec 17, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db4261e
fix-32310-Made changes to fix Android modal navigation race condition…
BagavathiPerumal 7d9605e
fix-32310-Added iOS and Android snapshots.
BagavathiPerumal bbd4996
fix-32310-Added Mac and Windows snapshots.
BagavathiPerumal 0100970
fix-32310-Made code changes to ensure PresentationCompleted event fir…
BagavathiPerumal 47bc4d6
fix-32310: Made code changes to add TaskCreationOptions.RunContinuati…
BagavathiPerumal e6fc556
fix-32310-Made changes to move FirePresentationCompleted to OnResume(…
BagavathiPerumal 3028c12
fix-32310-Windows snapshot updated.
BagavathiPerumal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,7 +107,7 @@ Task<Page> PopModalPlatformAsync(bool animated) | |
| return Task.FromResult(modal); | ||
| } | ||
|
|
||
| var source = new TaskCompletionSource<Page>(); | ||
| var source = new TaskCompletionSource<Page>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| if (animated && dialogFragment.View is not null) | ||
| { | ||
|
|
@@ -168,8 +168,6 @@ async Task PushModalPlatformAsync(Page modal, bool animated) | |
|
|
||
| async Task PresentModal(Page modal, bool animated) | ||
| { | ||
| TaskCompletionSource<bool> animationCompletionSource = new(); | ||
|
|
||
| var parentView = GetModalParentView(); | ||
|
|
||
| var dialogFragment = new ModalFragment(WindowMauiContext, modal) | ||
|
|
@@ -185,19 +183,32 @@ async Task PresentModal(Page modal, bool animated) | |
|
|
||
| if (animated) | ||
| { | ||
| dialogFragment!.AnimationEnded += OnAnimationEnded; | ||
| TaskCompletionSource<bool> animationCompletionSource = new(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| dialogFragment.AnimationEnded += OnAnimationEnded; | ||
|
|
||
| void OnAnimationEnded(object? sender, EventArgs e) | ||
| { | ||
| dialogFragment.AnimationEnded -= OnAnimationEnded; | ||
| animationCompletionSource.SetResult(true); | ||
| } | ||
|
|
||
| await animationCompletionSource.Task; | ||
| } | ||
| else | ||
| { | ||
| animationCompletionSource.TrySetResult(true); | ||
| } | ||
| // Non-animated modals need to wait for presentation completion to prevent race conditions | ||
| TaskCompletionSource<bool> presentationCompletionSource = new(); | ||
|
|
||
| void OnAnimationEnded(object? sender, EventArgs e) | ||
| { | ||
| dialogFragment!.AnimationEnded -= OnAnimationEnded; | ||
| animationCompletionSource.SetResult(true); | ||
| dialogFragment.PresentationCompleted += OnPresentationCompleted; | ||
|
|
||
| void OnPresentationCompleted(object? sender, EventArgs e) | ||
| { | ||
| dialogFragment.PresentationCompleted -= OnPresentationCompleted; | ||
| presentationCompletionSource.SetResult(true); | ||
| } | ||
|
|
||
| await presentationCompletionSource.Task; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -208,9 +219,10 @@ internal class ModalFragment : DialogFragment | |
| NavigationRootManager? _navigationRootManager; | ||
| static readonly ColorDrawable TransparentColorDrawable = new(AColor.Transparent); | ||
| bool _pendingAnimation = true; | ||
| bool _pendingNavigation = true; | ||
|
|
||
| public event EventHandler? AnimationEnded; | ||
|
|
||
| internal event EventHandler? AnimationEnded; | ||
| internal event EventHandler? PresentationCompleted; | ||
|
|
||
| public bool IsAnimated { get; internal set; } | ||
|
|
||
|
|
@@ -356,13 +368,25 @@ public override void OnStart() | |
| var dialog = Dialog; | ||
|
|
||
| if (dialog is null || dialog.Window is null || View is null) | ||
| { | ||
| // SAFETY: Fire event even on early return to prevent deadlock | ||
| FirePresentationCompleted(); | ||
| return; | ||
| } | ||
|
|
||
| int width = ViewGroup.LayoutParams.MatchParent; | ||
| int height = ViewGroup.LayoutParams.MatchParent; | ||
| dialog.Window.SetLayout(width, height); | ||
| } | ||
|
|
||
| public override void OnResume() | ||
| { | ||
| base.OnResume(); | ||
|
|
||
| // Signal that the modal is fully presented and ready | ||
| FirePresentationCompleted(); | ||
jfversluis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public override void OnDismiss(IDialogInterface dialog) | ||
| { | ||
| _modal.PropertyChanged -= OnModalPagePropertyChanged; | ||
|
|
@@ -385,6 +409,9 @@ public override void OnDestroy() | |
| { | ||
| base.OnDestroy(); | ||
| FireAnimationEnded(); | ||
|
|
||
| // SAFETY: If destroyed before OnStart completed, fire PresentationCompleted to prevent deadlock | ||
| FirePresentationCompleted(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if it's a good idea to complete the TCS, I would say the best here is to Cancel it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| void FireAnimationEnded() | ||
|
|
@@ -398,6 +425,15 @@ void FireAnimationEnded() | |
| AnimationEnded?.Invoke(this, EventArgs.Empty); | ||
| } | ||
|
|
||
| void FirePresentationCompleted() | ||
| { | ||
| if (!_pendingNavigation) | ||
| return; | ||
|
|
||
| _pendingNavigation = false; | ||
| PresentationCompleted?.Invoke(this, EventArgs.Empty); | ||
| } | ||
|
|
||
|
|
||
| sealed class CustomComponentDialog : ComponentDialog | ||
| { | ||
|
|
||
Binary file added
BIN
+10.2 KB
...ests/TestCases.Android.Tests/snapshots/android/ModalNavigationShouldNotHang.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 32310, "App hangs if PopModalAsync is called after PushModalAsync with single await Task.Yield()", PlatformAffected.Android)] | ||
| public class Issue32310 : ContentPage | ||
| { | ||
| public Issue32310() | ||
| { | ||
| var navigateButton = new Button | ||
| { | ||
| Text = "Perform Modal Navigation", | ||
| AutomationId = "NavigateButton", | ||
| VerticalOptions = LayoutOptions.Center, | ||
| HorizontalOptions = LayoutOptions.Center | ||
| }; | ||
|
|
||
| navigateButton.Clicked += (s, e) => | ||
| { | ||
| Dispatcher.DispatchAsync(async () => | ||
| { | ||
| await Navigation.PushModalAsync(new ContentPage() { Content = new Label() { Text = "Hello!" } }, false); | ||
|
|
||
| await Task.Yield(); | ||
|
|
||
| await Navigation.PopModalAsync(); | ||
| }); | ||
| }; | ||
|
|
||
| var layout = new VerticalStackLayout | ||
| { | ||
| Padding = new Thickness(30, 0), | ||
| Spacing = 25, | ||
| VerticalOptions = LayoutOptions.Center, | ||
| HorizontalOptions = LayoutOptions.Center, | ||
| Children = | ||
| { | ||
| navigateButton | ||
| } | ||
| }; | ||
|
|
||
| Content = layout; | ||
| } | ||
| } |
Binary file added
BIN
+7.2 KB
...ntrols/tests/TestCases.Mac.Tests/snapshots/mac/ModalNavigationShouldNotHang.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions
24
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32310.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue32310 : _IssuesUITest | ||
| { | ||
| public Issue32310(TestDevice device) : base(device) | ||
| { | ||
| } | ||
|
|
||
| public override string Issue => "App hangs if PopModalAsync is called after PushModalAsync with single await Task.Yield()"; | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.Navigation)] | ||
| public void ModalNavigationShouldNotHang() | ||
| { | ||
| App.WaitForElement("NavigateButton"); | ||
| App.Tap("NavigateButton"); | ||
| App.WaitForElement("NavigateButton"); | ||
| VerifyScreenshot(); | ||
| } | ||
| } |
Binary file added
BIN
+7.21 KB
.../tests/TestCases.WinUI.Tests/snapshots/windows/ModalNavigationShouldNotHang.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+22.9 KB
...ntrols/tests/TestCases.iOS.Tests/snapshots/ios/ModalNavigationShouldNotHang.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say we don't need this here. I added those
nullcheck here to make the nullable analyser happy. If this results intrue, something is very wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pictos, I have added FirePresentationCompleted() in the null check condition to prevent potential deadlocks as per @PureWeen's suggestion. Please find the below comment for your reference.
Comment link: #32479 (review)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BagavathiPerumal I read it, but not sure if we just want to complete the Task or set an exception to the user, since the navigation will be in a invalid state.
cc: @PureWeen