3

I am having an issue with a MAUI app where very rarely, I get an unhandled NavigationFailed exception. I have one main view, which navigates to a secondary page with the following:

await Shell.Current.GoToAsync($"{nameof(AddCollectionPage)}", true, new Dictionary<string, object>
{
    {"Contracts", queryStringBuilder.ToString() }
});

then there is a button within that second page which returns backwards, using the following:

await Shell.Current.GoToAsync("../");

Now this works fine, and probably 999 out of 1000 times there are no issues... But rarely, the app crashes. Whilst debugging, I it breaks at the UnhandledException point within App.g.i.cs at this code:

UnhandledException += (sender, e) =>
{
    if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};

The exception isn't all that useful. enter image description here

The call stack consists of just 4 entries - 2 of which are external code, 1 is the location I posted within App.g.i.cs and the other is the line where it calls the shell navigation.

This was hit just now, at the point in the code where I called Navigation to go to "../" from the secondary page back to the main one. Its a really rare occurrence, but frequent enough to be pretty annoying.

When I wrap the line calling GoToAsync which causes the crash in a try catch and log the exception it catches, I get the following info (error and stack trace). The bottom line of which references the part of my code calling GoToAsync, with everything else being MAUI framework stuff.

System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.IFrameMethods.GoBack(IObjectReference _obj, NavigationTransitionInfo transitionInfoOverride)
   at Microsoft.Maui.Handlers.ElementHandler.Invoke(String command, Object args)
   at Microsoft.Maui.Controls.ShellSection.OnPopAsync(Boolean animated)
   at Microsoft.Maui.Controls.ShellSection.PrepareCurrentStackForBeingReplaced(ShellNavigationRequest request, ShellRouteParameters queryData, IServiceProvider services, Nullable`1 animate, List`1 globalRoutes, Boolean isRelativePopping)
   at Microsoft.Maui.Controls.ShellSection.GoToAsync(ShellNavigationRequest request, ShellRouteParameters queryData, IServiceProvider services, Nullable`1 animate, Boolean isRelativePopping)
   at Microsoft.Maui.Controls.ShellNavigationManager.GoToAsync(ShellNavigationParameters shellNavigationParameters, ShellNavigationRequest navigationRequest)
   at BlurFarm.ViewModels.AddCollectionPageViewModel.SelectCollection(Collection collection)
pingu2k4
  • 956
  • 2
  • 15
  • 31
  • I suspect there might be some Maui timing bug. "Run without debugger". When no debugger, code jit-compiles, runs faster. Still fail sometimes? – ToolmakerSteve Mar 23 '23 at 15:43
  • I've had the same crash in release mode with no debugger, if that helps @ToolmakerSteve – pingu2k4 Mar 23 '23 at 18:06
  • I added a more detailed stack trace, given when catching the error with a try catch around the call to GoToAsync – pingu2k4 Mar 23 '23 at 18:09

1 Answers1

0

Sure looks like a Maui bug.

  • I recommend making a public github repo, containing a simple project with just enough code to run and show the problem. Make a new project from the Maui template, and keep the two pages simple. Still happen?

  • See if this hack avoids the problem:

MainThread.BeginInvokeOnMainThread(async () =>
{
   await Task.Delay(200);
   await Shell.Current.GoToAsync("../");
}

This does two things:

  1. Lets the button method return before executing anything.
  2. Introduces a slight delay.

My thinking is that Maui is sometimes in a state where it is "not ready" to perform that GoToAsync. I don't have a mental picture to explain how this could be the case, but its worth a try.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
  • Thanks for this. The problem with making a public repo with a simple reproduction is that I am seeing it happen so infrequently its really difficult to nail down what the exact conditions are, and it could take forever to verify whether there is an issue or not with a specific repo. I only see the issue often enough currently as I am constantly testing things etc in the app I am building. The most recent times seem to all have been navigating backwards using "../", and I can't remember before that seeing it happen going forwards... will try your code above though, thanks. – pingu2k4 Mar 23 '23 at 22:11