maui: WinRt exception when page navigated
Description
When the app navigate between two pages randomly crashes. In the second page my app is trying to deserialize a json file to runtime create the UI.
Steps to Reproduce
- Run the sample
- Navigate for a while from “MainPage” to “Page2”
- Watch the app crash
To speed up the process I’ve used a tool for automatic clicking ([https://tinytask.net/download.html])
Link to public reproduction project repository
https://github.com/davide-cavallini/WinRtExceptionTest
Version with bug
8.0.40 SR5
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
No response
Did you find any workaround?
Didn’t find a specific workaround, but calling Task.Sleep(n) before navigation seems to reduce the frequency of exception happening.
Relevant log output
'WinRtExceptionTest.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Collections.Immutable.dll'.
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
at ABI.Microsoft.UI.Xaml.Controls.IContentPresenterMethods.set_Content(IObjectReference _obj, Object value)
at Microsoft.Maui.Platform.StackNavigationManager.OnNavigated(Object sender, NavigationEventArgs e)
at ABI.Microsoft.UI.Xaml.Navigation.NavigatedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)
--- End of stack trace from previous location ---
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
at ABI.Microsoft.UI.Xaml.Controls.IFrameMethods.GoBack(IObjectReference _obj, NavigationTransitionInfo transitionInfoOverride)
at Microsoft.Maui.Platform.StackNavigationManager.NavigateTo(NavigationRequest args)
at Microsoft.Maui.CommandMapper.InvokeCore(String key, IElementHandler viewHandler, IElement virtualView, Object args)
at Microsoft.Maui.Controls.Handlers.ShellSectionHandler.SyncNavigationStack(Boolean animated, NavigationRequestedEventArgs e)
at Microsoft.Maui.Controls.ShellSection.OnPopAsync(Boolean animated)
at Microsoft.Maui.Controls.ShellSection.GoToAsync(ShellNavigationRequest request, ShellRouteParameters queryData, IServiceProvider services, Nullable`1 animate, Boolean isRelativePopping)
at Microsoft.Maui.Dispatching.DispatcherExtensions.<>c__DisplayClass3_0.<<DispatchAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.Maui.Dispatching.DispatcherExtensions.<>c__DisplayClass2_0`1.<<DispatchAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.Maui.Controls.ShellNavigationManager.GoToAsync(ShellNavigationParameters shellNavigationParameters, ShellNavigationRequest navigationRequest)
at WinRtExceptionTest.Page2.<AddContainer>b__4_0()
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 26 (7 by maintainers)
I believe it’s not possible to reproduce this issue in a plain WinUI using Frame control, just because exception occurs when MAUI’s StackNavigationManager tries to set content into the page presenter (and it’s a good question what was the intense to implement navigation in this way): https://github.com/dotnet/maui/blob/049ec597534a77c0c57a0207c856453387f1f54b/src/Core/src/Platform/Windows/StackNavigationManager.cs#L183 Plain WinUI navigation approach with Frame control doesn’t need this manipulation and works differently.
This issue just happened also to me, the Windows app uses Maui 8.0.40 and I was doing a back navigation with
await Shell.Current.GoToAsync("..");There is a first unhandled exception with empty text in Exception field and the following text in Message field:
Microsoft.UI.Xaml.Controls.Frame.NavigationFailed was unhandled.Then a second exception is thrown:
After that, the same back navigation does not work anymore, but it’s possible to successfully navigate to another page by clicking a Shell FlyoutItem, then the navigation seems to restart working as before.
If it can be useful information, another way to mitigate the frequency of error (please note, mitigate, not avoid) is to place a little delay before navigating:
Task.Delay(300) or Task.Sleep()
Catching the exception is possibile, but at that point navigation no longer works and you need to kill and restart app.
I came up with this workaround 6 months ago and if I remember correctly I put it there only as a fallback if the first retry group fails and setting the new MauiContext helped.
The crashes are bound to back navigation but sometimes it also crashes after navigating back and to some other place.
Yes, it is not superb having to dig in Maui and look for workarounds, we have quite a lot of them, especially for WinUI3.
We have experienced this and came up with a workaround. The initial fix was creating our own StackNavigationManager that would navigate with Polly retries with just repeating the setting of the page’s content, if that fails then the process is repeated with a new mauicontext.
`internal sealed class ExtendedNavigationViewHandler : NavigationViewHandler { private StackNavigationManager? stackNavigationManager;
}`
To use this handler, register it builder.ConfigureMauiHandlers(handlers => handlers.AddHandler<NavigationPage, ExtendedNavigationViewHandler>()); If there are others just chain the call. It was better than before but we have kept getting crashes where VS debugging did not help at all, it would just show the COMException without relevant stacktrace even if we have the pdbs copied to the app and can debug external or native code, so the WinDbg was used to see what is going on. We found out about this https://github.com/microsoft/microsoft-ui-xaml/issues/9070 that would fast kill the process without any chance of recovery. So the workaround is not to use the text services at all until the issue in WinUI3 gets fixed. To disable them use this and call it somewhere during the maui startup.
`///
/// Delete after this is done https://github.com/microsoft/microsoft-ui-xaml/issues/9070
/// Default for <see cref="ITextInput.IsSpellCheckEnabled"/> and <see cref="ITextInput.IsTextPredictionEnabled"/> in WinUI is true and for MAUI it also defaults to true.
///
private static void DisableTextServices()
{
// the discarded action calls UpdateIsSpellCheckEnabled but to protect ourselves from future modification in that corresponding action we call it manually
EditorHandler.Mapper.ModifyMapping(nameof(IEditor.IsSpellCheckEnabled), (handler, editor, _) =>
{
((InputView)editor).IsSpellCheckEnabled = false;
((InputView)editor).IsTextPredictionEnabled = false;
handler.PlatformView?.UpdateIsSpellCheckEnabled(editor);
});
}`
With these two in place the random crashes are gone. But we still get some crashes from our customers where the stacktrace is just COMException without anything relevant to pinpoint the crash place. We suspect that it is due to some memory leaks from CsWinRT, WinUI3 and MAUI but we are not sure because we can not reproduce these crashes in our environment even with restricted VMs.
Hi @jaosnz-rep. The repo was private. Now it’s public. My bad, sorry for the inconvenience.