From bd031d6cce350ea841aada110490a62d8d4492d6 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com> Date: Sun, 29 Nov 2026 17:39:16 +0100 Subject: [PATCH] [Windows] Resume NavigationPage outside native completion callbacks (#40802) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Agent assessment Self-reported assessment, not an independent review. Assessed revision: `e9964a9acf9cfa240b69eba1155898bc524dcc6b` | Dimension | Assessment | Rationale | |---|---|---| | Change confidence | 8/10 | ↑ Deferral is compiled only for Windows; other platforms retain main's completion behavior.
↑ 350 navigation unit tests pass, including the non-Windows ordering regression test.
↑ Build 1704240 validates iOS General and Windows NavigationPage in both packaging modes at this exact head.
↓ The first scoped-head run still had iOS failures; an unchanged-head retry passed, so native environment variability remains relevant. | | Diagnosis confidence | 7/10 | ↑ WinUI completes navigation inside Loaded, where inline callers can immediately close the window.
↑ The Windows-only change retains success of the original crash test in both packaging modes.
↑ Non-Windows callback ordering is restored and unit-tested.
↓ The original Windows native faulting module is unknown; the 17 iOS failures cannot all be causally attributed to deferral, since they also occurred once with this scoped head. | | Evidence fit | 8/10 | ↑ Build 1704240 genuine TRX: iOS General 1479 passed/0 failed/14 existing skips, including all 17 previously failing cases.
↑ Both Windows NavigationPage categories: 16 passed/0 failed/3 existing skips, and the original test explicitly Passed (242 ms packaged, 240 ms unpackaged).
↓ Build 1704232's first scoped-head iOS result was red; the green validation is the unchanged-head retry 1704240, not that earlier run. | > [!NOTE] > Are you waiting for the changes in this PR to be merged? > It would be very helpful if you could [test the resulting artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from this PR and let us know in a comment if this change resolves your issue. Thank you! ### Target Branch | This PR is for | Initial base | | --- | --- | | New features, API changes, or .NET 12 work (including .NET 12 fixes) | `main` | | .NET 11 servicing fixes | `net11`, for review | | Older servicing, release integration, infrastructure, or documentation | The branch designated for that work | - [x] I selected the applicable base branch using the [contribution guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting): `main`, explicitly requested for the .NET 12 Windows CI failure. For .NET 11 fixes, only a maintainer retargets the approved PR to the active inflight lane and merges it after CI validates that final base. Do not merge individual fixes directly into `net11`; agents must not retarget or merge. ### Description of Change On **Windows only**, create the active `NavigationPage` request completion source with `RunContinuationsAsynchronously`. Other platforms retain the original completion source and native callback ordering. On Windows, `StackNavigationManager.OnNavigated` completes the request from WinUI's `Loaded` callback. Previously, `SetResult` could synchronously run the entire navigation continuation and its caller while that native callback was still on the stack. In `SettingTitleIconImageSourceDoesntCrash`, this includes updating the toolbar image and immediately closing/destroying the window through `CreateHandlerAndAddToWindow`. Queueing continuations lets the native completion callback unwind first. The updated portable unit test uses a manually completed handler and verifies that non-Windows `NavigatedTo` retains its original synchronous native callback ordering. It fails against this PR's earlier unscoped deferral and passes with the Windows-only correction. The existing Windows device test is unchanged: no skip, delay, image substitution, or reduced coverage. Baseline: [build 1704198](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1704198), Windows Controls packaged [log 760](https://dev.azure.com/dnceng-public/public/_apis/build/builds/1704198/logs/760?api-version=7.1), 11:38:15–11:38:38 UTC. The recovered TRX contains one passed test; the sequence starts `SettingTitleIconImageSourceDoesntCrash` without an end event. The process exits 57005 (0xDEAD), MTP exits 7. The artifact contains no `.dmp`, and `windows-application-events.json` is empty. No faulting module or native exception is known. The supplied additional baseline builds are 1704155, 1703964 and 1704185. Duplicate search covered open and recently merged PRs across main, net11, release, candidate and inflight branches for this test, title icons, PageControl, StackNavigationManager, teardown, and navigation continuations. This is not #40795's initial-navigation event guard (already on main), nor #39911's stale image-result guard (also already on main). Validation (latest revision, configured SDK `12.0.100-alpha.1.26523.103`, macOS): ```sh dotnet test src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj \ -p:IncludeAndroidTargetFrameworks=false -p:IncludeIosTargetFrameworks=false \ -p:IncludeMacCatalystTargetFrameworks=false -p:IncludeMacOSTargetFrameworks=false \ -p:IncludeWindowsTargetFrameworks=false \ --filter-method '*NonWindowsNavigationCompletionPreservesNativeCallbackOrdering' --verbosity quiet ``` - Before scoping the fix: **failed**, `Assert.True`, actual `False` (1 test), proving the unscoped PR changes non-Windows ordering. - After scoping, same command with `--filter-method '*Navigation*'`: **passed**, 350/350. - `dotnet format whitespace --folder --include src/Controls/src/Core/NavigationPage/NavigationPage.cs src/Controls/tests/Core.UnitTests/NavigationPageLifecycleTests.cs --verbosity quiet`: **passed**. - `git diff --check`: **passed**. - Latest-head native iOS/Windows CI: **validated in build 1704240** below; not run locally. #### Native CI history and regression correction [Build 1704212](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1704212) tested the **previous** head `c4da77aa65cbec06d93b41a8c4b0b6ae69f219e6` (merge `6da2433c9f8cde06366718a44c461b11bc97caf7`): - Windows NavigationPage passed in both modes: 16 passed, 0 failed, 3 existing skips each. Both genuine TRX files explicitly mark `SettingTitleIconImageSourceDoesntCrash` Passed (148 ms packaged, 189 ms unpackaged). [Packaged log 760](https://dev.azure.com/dnceng-public/public/_apis/build/builds/1704212/logs/760?api-version=7.1); [unpackaged log 739](https://dev.azure.com/dnceng-public/public/_apis/build/builds/1704212/logs/739?api-version=7.1). - **Observed failures:** iOS Controls CoreCLR General [log 727](https://dev.azure.com/dnceng-public/public/_apis/build/builds/1704212/logs/727?api-version=7.1) failed 17/1493 tests: TabbedPage background transitions, NavigationPage back-button observation, More-tab PageTransitions, and ScrollView horizontal options. The unscoped deferral changed non-Windows callback ordering (confirmed by the unit test); this revision restores main's behavior outside Windows. The 17 native failures are not conclusively all caused by that ordering change: they also recurred in the first Windows-only run before passing on the unchanged-head retry. - The reported Windows unpackaged Shell crash and Windows BlazorWebView initialization failure also occur on main / are known flakes; they are outside this fix and are not modified or hidden. - Build 1704210 was canceled before execution and is not validation evidence. #### Latest-head native validation: build 1704240 [Build 1704240](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1704240) validates head `e9964a9acf9cfa240b69eba1155898bc524dcc6b` through merge `4edb3a18f4e96fe3be0f866b887b506e1a42a221`. | Selected coverage | Actual result | Raw task log | | --- | --- | --- | | iOS Controls CoreCLR General | 1479 passed, 0 failed, 14 existing skips; job succeeded | [736](https://dev.azure.com/dnceng-public/public/_apis/build/builds/1704240/logs/736?api-version=7.1) | | Windows Controls packaged NavigationPage | 16 passed, 0 failed, 3 existing skips; category exit 0 | [745](https://dev.azure.com/dnceng-public/public/_apis/build/builds/1704240/logs/745?api-version=7.1) | | Windows Controls unpackaged NavigationPage | 16 passed, 0 failed, 3 existing skips; category exit 0 | [739](https://dev.azure.com/dnceng-public/public/_apis/build/builds/1704240/logs/739?api-version=7.1) | The genuine iOS `native/testResults.trx` confirms all 17 previously failing cases individually **Passed**. Both Windows `categories/NavigationPage/Controls-NavigationPage.trx` files explicitly record `SettingTitleIconImageSourceDoesntCrash` **Passed** (242 ms packaged, 240 ms unpackaged). No native device tests were changed or newly skipped. **First-run caveat:** build 1704232 tested the same scoped head and still had the same 17 iOS failures (log 736), despite compiling IOS rather than WINDOWS and retaining main's non-Windows code. Compared with green main 1704155, its simulator log records a different iOS 27.2 runtime build (`SimulatorRuntime-v24.2.91016.19` versus `v24.2.8117.0`), and provisioning ends with `Data Migration Failed`. These differences support native environment variability, not a proven root cause of every failure. The subsequent 1704240 run was an **unchanged-head retry**, not another product fix. The packaged Windows job in 1704240 still fails in the separate Shell category (exit 7); NavigationPage passes. The known Shell/BlazorWebView failures are not fixed or hidden here. This is validation of the requested iOS General and Windows NavigationPage coverage, not a full-CI-green claim. ### Issues Fixed Addresses the Windows Controls `SettingTitleIconImageSourceDoesntCrash` CI host crash described above; no separate issue was supplied. Related investigation: #40795, not a replacement or duplicate of its net11 fix. ### Let AI finish this PR - [ ] **Let the MAUI agent address review feedback and fix remaining issues on this PR.** Maintainers still decide whether it is merged. For PRs from forks, the agent opens a follow-up PR that keeps your commits. --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 2f147b35e577472c19ace62f1cd1a31515a1497a) --- .../src/Core/NavigationPage/NavigationPage.cs | 6 +++++ .../NavigationPageLifecycleTests.cs | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Controls/src/Core/NavigationPage/NavigationPage.cs b/src/Controls/src/Core/NavigationPage/NavigationPage.cs index 63792809b02c..2ed66544a619 100644 --- a/src/Controls/src/Core/NavigationPage/NavigationPage.cs +++ b/src/Controls/src/Core/NavigationPage/NavigationPage.cs @@ -763,7 +763,13 @@ async Task SendHandlerUpdateAsync( // If our handler was removed while waiting then don't do anything if (Handler != null) { +#if WINDOWS + // WinUI completes navigation inside Loaded; avoid running window closure or + // another navigation inline in that native callback. + var currentNavRequestTaskSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); +#else var currentNavRequestTaskSource = new TaskCompletionSource(); +#endif _allPendingNavigationCompletionSource ??= new TaskCompletionSource(); if (CurrentNavigationTask == null) diff --git a/src/Controls/tests/Core.UnitTests/NavigationPageLifecycleTests.cs b/src/Controls/tests/Core.UnitTests/NavigationPageLifecycleTests.cs index 0ab5b11d5a47..c6db5b810b09 100644 --- a/src/Controls/tests/Core.UnitTests/NavigationPageLifecycleTests.cs +++ b/src/Controls/tests/Core.UnitTests/NavigationPageLifecycleTests.cs @@ -93,6 +93,31 @@ public async Task AppearingFiresForInitialPage(bool useMaui) Assert.Equal(resultPage, contentPage); } + [Fact] + public async Task NonWindowsNavigationCompletionPreservesNativeCallbackOrdering() + { + var nav = new NavigationPage(new ContentPage()); + var handler = new ManualNavigationHandler(); + handler.SetMauiContext(new MauiContext(Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(new Microsoft.Extensions.DependencyInjection.ServiceCollection()))); + nav.Handler = handler; + await handler.WaitForRequestsAsync(1); + handler.Finish(0); + + var page = new ContentPage(); + var push = nav.PushAsync(page); + await handler.WaitForRequestsAsync(2); + var finishingThread = Environment.CurrentManagedThreadId; + var insideNativeCallback = true; + var reentered = false; + page.NavigatedTo += (_, _) => + reentered = insideNativeCallback && Environment.CurrentManagedThreadId == finishingThread; + + handler.Finish(1); + insideNativeCallback = false; + await push.WaitAsync(TimeSpan.FromSeconds(5)); + Assert.True(reentered); + } + [Theory] [InlineData(false)] [InlineData(true)]