Repository navigation
Conversation
…r/DragLeave (dotnet#40441) <!-- Please let the below note in for people that find this PR --> > [!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! <!-- Open .NET 11 fix PRs against net11 for review; target main for next-major features. After approval, a maintainer manually retargets the fix PR to the active inflight branch and merges it directly there. Do not merge individual fixes into net11. Bots must not retarget or merge. Candidate integration into net11 and release/* changes are separate, explicitly coordinated release work. --> ### 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). 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 iOS, `DragEventArgs.GetPosition()` can throw a `NullReferenceException` inside a `DragOver` or `DragLeave` handler. The exception crashes the app. **CI evidence.** The `DropEventCoordinates` UI test crashed the HostApp on both attempts on the iOS CoreCLR lane of SR2 candidate dotnet#40116 (maui-pr-uitests [1690394](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1690394), iOS latest Editor..Fr job). The job's `.ips` reports show SIGABRT from `xamarin_process_managed_exception` under `-[UIDropInteraction _dragDestinationGestureStateChanged:]`. The logarchive shows the managed exception: ``` System.NullReferenceException at DragAndDropDelegate.CalculatePosition(IElement, WeakReference, WeakReference) at DragAndDropBetweenLayouts.OnDragOver(...) // HostApp calls e.GetPosition(...) at DragAndDropDelegate.<HandleDragOver>... at DragAndDropDelegate.SendEventArgs(...) at DragAndDropDelegate.HandleDragOver(...) at DragAndDropDelegate.SessionDidUpdate(...) ``` **Root cause.** - `SessionDidUpdate` and `SessionDidExit` pass `session.LocalDragSession`, a transient managed wrapper of the native local drag session, into `HandleDragOver` and `HandleDragLeave`. - Those methods put only a `WeakReference` to it in the `GetPosition` callback, and nothing else on the path keeps it alive: `PlatformDragEventArgs` holds the outer `IUIDropSession`, not the local drag session. - A GC while the user's handler is running can therefore collect the wrapper. `CalculatePosition` then calls `LocationInView` on a null session. - Separately, `HandleDragOver` built a weak reference even when `LocalDragSession` is null (a drag from another app), so `GetPosition` always threw in that case. `HandleDragLeave` and `HandleDrop` already return null there. The weak references date from 2023, and the code is identical on `net11`, the candidate and `main`. This is a latent bug that the CoreCLR lane's GC timing exposed, not a candidate regression. **Fix** (`DragAndDropDelegate.cs`, iOS/Mac Catalyst): - `HandleDragOver` and `HandleDragLeave` call `GC.KeepAlive(session)` after dispatching to the recognizers. The session stays alive while handlers can synchronously call `GetPosition`, and the event args still don't hold it strongly. - `HandleDragOver` uses a null `sessionRef` when there is no local drag session, matching `HandleDragLeave` and `HandleDrop`. - `CalculatePosition` returns `null` when the session reference is missing or its target has already been collected, instead of dereferencing null. This covers a `GetPosition` call made after the handler returns. **Tests** (`DragAndDropTests.iOS.cs`, `Gesture` category): - `DragPositionIsAvailableAfterGarbageCollectionDuringHandler(dragLeave: false/true)`: a fake drop session returns a fresh local drag session wrapper each time, the handler forces a full GC and then calls `GetPosition`, and the test asserts the fake session's location. - `DragOverPositionIsNullWithoutLocalDragSession`: with no local drag session, `GetPosition` in `DragOver` returns `null`. **Validation** (iOS 18.5 simulator, Release, CoreCLR, the same runtime as the failing CI lane): | Command | Result | | --- | --- | | `pwsh .github/skills/run-device-tests/scripts/Run-DeviceTests.ps1 -Project Controls -Platform ios -TestFilter "Category=Gesture" -DeviceUdid <iOS 18.5 sim> -SkipXcodeVersionCheck` with only the new tests (no fix) | **Failed**: 45 passed, 3 failed. All three new cases throw `NullReferenceException` at `DragAndDropDelegate.CalculatePosition`, the same frame as the CI crash. | | Same command with the fix | **Passed**: 48 passed, 0 failed | | `dotnet format whitespace --folder --include <changed files> --verify-no-changes` | Passed | The UI test itself was not rerun locally, because its crash depends on when a GC happens. ### Issues Fixed No existing issue; found while investigating SR2 candidate dotnet#40116 CI. Searched for related issues and PRs and found no duplicate. dotnet#28416/dotnet#28417 is an unrelated Mac Catalyst `DragAndDropDelegate` NRE. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 40452Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 40452" |
|
Hey there @MykhailoDav! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
PR Test Gate
🧪 Gate analysis — click to expand⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect. No interpretable test-group summary was produced. No additional test execution is inferred. Setup did not capture a verified PR head; no commit is claimed as tested. No immutable without-fix baseline was prepared. |
There was a problem hiding this comment.
Expert Review — 0 findings
Reviewed commit 6074706.
) ### Description of Change An iOS app that sets `MainPage` and has a scene manifest crashes with `InvalidOperationException: Window was already destroyed` every time the user swipes it away in the app switcher. When the scene disconnects, `SceneDidDisconnect` calls `Destroying()` on the window. `Application.RemoveWindow` returns early for the `MainPage` window, so that window, already destroyed, stays in `Application.Windows`. UIKit then calls `applicationWillTerminate`, and the `WillTerminate` handler calls `app.GetWindow()?.Destroying()` on it a second time. Its comment assumes `GetWindow()` is null by then, which isn't true for `MainPage` apps. `WillTerminate` now skips a window whose handler is already disconnected, which is what `Destroying()` leaves behind. If terminate arrives without a prior scene disconnect, the window is still destroyed. I chose this over the `!HasSceneManifest()` guard the neighbouring handlers use because it depends on the window's state, not on Info.plist. #24191 was closed with "use CreateWindow", but the scene manifest is now in the .NET 11 templates and required by the iOS 27 SDK, so every app that still uses `MainPage` hits this crash on force-close. ### Tests `WindowTests.WillTerminateDestroysMainPageWindowOnce` (Controls device test, Window category, iOS and Mac Catalyst) sets `MainPage`, connects the window, optionally calls `Destroying()` as `SceneDidDisconnect` does, then runs the registered `WillTerminate` handlers. It checks that nothing throws and that `Destroying` runs exactly once in both cases. On the iPhone 17 simulator (iOS 26.5), Window category: 53/57 without the fix (the 4 failures are the scene-disconnect cases, with the same stack as the issue), 57/57 with it. Application 1/1 and Lifecycle 9/9 with the fix. **Before / after** (sandbox app with `MainPage` set, Release build, swiped away in the app switcher with Appium; the app logs a line after MAUI's own handlers finish). There's no video because the app disappears either way. Before: ``` [KeyWindow] Scene will invalidate: UIWindowScene ... [40438] SceneDidDisconnect handlers finished *** Terminating app due to uncaught exception 'System.InvalidOperationException', reason: 'Window was already destroyed (System.InvalidOperationException) at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Destroying() at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_1(UIApplication app) at Microsoft.Maui.MauiUIApplicationDelegate.WillTerminate(UIApplication application) ``` After: ``` [KeyWindow] Scene will invalidate: UIWindowScene ... [40438] SceneDidDisconnect handlers finished [40438] WillTerminate handlers finished ``` `xcrun simctl terminate` kills the process without calling `applicationWillTerminate`, so it doesn't reproduce this. Not checked: Mac Catalyst (no local workload) and a physical device. ### Issues Fixed Fixes #40438 --------- Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
) ### Description of Change An iOS app that sets `MainPage` and has a scene manifest crashes with `InvalidOperationException: Window was already destroyed` every time the user swipes it away in the app switcher. When the scene disconnects, `SceneDidDisconnect` calls `Destroying()` on the window. `Application.RemoveWindow` returns early for the `MainPage` window, so that window, already destroyed, stays in `Application.Windows`. UIKit then calls `applicationWillTerminate`, and the `WillTerminate` handler calls `app.GetWindow()?.Destroying()` on it a second time. Its comment assumes `GetWindow()` is null by then, which isn't true for `MainPage` apps. `WillTerminate` now skips a window whose handler is already disconnected, which is what `Destroying()` leaves behind. If terminate arrives without a prior scene disconnect, the window is still destroyed. I chose this over the `!HasSceneManifest()` guard the neighbouring handlers use because it depends on the window's state, not on Info.plist. #24191 was closed with "use CreateWindow", but the scene manifest is now in the .NET 11 templates and required by the iOS 27 SDK, so every app that still uses `MainPage` hits this crash on force-close. ### Tests `WindowTests.WillTerminateDestroysMainPageWindowOnce` (Controls device test, Window category, iOS and Mac Catalyst) sets `MainPage`, connects the window, optionally calls `Destroying()` as `SceneDidDisconnect` does, then runs the registered `WillTerminate` handlers. It checks that nothing throws and that `Destroying` runs exactly once in both cases. On the iPhone 17 simulator (iOS 26.5), Window category: 53/57 without the fix (the 4 failures are the scene-disconnect cases, with the same stack as the issue), 57/57 with it. Application 1/1 and Lifecycle 9/9 with the fix. **Before / after** (sandbox app with `MainPage` set, Release build, swiped away in the app switcher with Appium; the app logs a line after MAUI's own handlers finish). There's no video because the app disappears either way. Before: ``` [KeyWindow] Scene will invalidate: UIWindowScene ... [40438] SceneDidDisconnect handlers finished *** Terminating app due to uncaught exception 'System.InvalidOperationException', reason: 'Window was already destroyed (System.InvalidOperationException) at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Destroying() at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_1(UIApplication app) at Microsoft.Maui.MauiUIApplicationDelegate.WillTerminate(UIApplication application) ``` After: ``` [KeyWindow] Scene will invalidate: UIWindowScene ... [40438] SceneDidDisconnect handlers finished [40438] WillTerminate handlers finished ``` `xcrun simctl terminate` kills the process without calling `applicationWillTerminate`, so it doesn't reproduce this. Not checked: Mac Catalyst (no local workload) and a physical device. ### Issues Fixed Fixes #40438 --------- Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ow in WillTerminate (#40554) Backport of #40452 to .NET 10 servicing. Fixes #40438 for .NET 10. The iOS 27 SDK makes the UIScene lifecycle mandatory, so every .NET 10 MAUI iOS app built with Xcode 27 needs a UIApplicationSceneManifest. Apps that still set Application.MainPage then crash with "InvalidOperationException: Window was already destroyed" on every force-close from the app switcher: SceneDidDisconnect destroys the window, the destroyed MainPage window stays in Application.Windows, and the WillTerminate handler destroys it again. Seen in production crash reports of a shipping .NET 10 app (MAUI 10.0.110, iOS 27.2.1). Changes (cherry-picked from #40452): - WillTerminate only destroys a window whose handler is still connected (Destroying() disconnects it), so a window the scene already destroyed is skipped; a terminate without a prior scene disconnect still destroys the window. - WindowTests.WillTerminateDestroysMainPageWindowOnce (iOS / Mac Catalyst device test), both with and without a prior scene disconnect. The test file had a .NET 11 only test in the original PR; in release/10.0.1xx-sr14 the iOS WindowTests partial was empty, so only the new test and its stub were added. --------- Co-authored-by: Mykhailo Davydenko <davudenko.mukhailo@gmail.com>
…net#40452) ### Description of Change An iOS app that sets `MainPage` and has a scene manifest crashes with `InvalidOperationException: Window was already destroyed` every time the user swipes it away in the app switcher. When the scene disconnects, `SceneDidDisconnect` calls `Destroying()` on the window. `Application.RemoveWindow` returns early for the `MainPage` window, so that window, already destroyed, stays in `Application.Windows`. UIKit then calls `applicationWillTerminate`, and the `WillTerminate` handler calls `app.GetWindow()?.Destroying()` on it a second time. Its comment assumes `GetWindow()` is null by then, which isn't true for `MainPage` apps. `WillTerminate` now skips a window whose handler is already disconnected, which is what `Destroying()` leaves behind. If terminate arrives without a prior scene disconnect, the window is still destroyed. I chose this over the `!HasSceneManifest()` guard the neighbouring handlers use because it depends on the window's state, not on Info.plist. dotnet#24191 was closed with "use CreateWindow", but the scene manifest is now in the .NET 11 templates and required by the iOS 27 SDK, so every app that still uses `MainPage` hits this crash on force-close. ### Tests `WindowTests.WillTerminateDestroysMainPageWindowOnce` (Controls device test, Window category, iOS and Mac Catalyst) sets `MainPage`, connects the window, optionally calls `Destroying()` as `SceneDidDisconnect` does, then runs the registered `WillTerminate` handlers. It checks that nothing throws and that `Destroying` runs exactly once in both cases. On the iPhone 17 simulator (iOS 26.5), Window category: 53/57 without the fix (the 4 failures are the scene-disconnect cases, with the same stack as the issue), 57/57 with it. Application 1/1 and Lifecycle 9/9 with the fix. **Before / after** (sandbox app with `MainPage` set, Release build, swiped away in the app switcher with Appium; the app logs a line after MAUI's own handlers finish). There's no video because the app disappears either way. Before: ``` [KeyWindow] Scene will invalidate: UIWindowScene ... [40438] SceneDidDisconnect handlers finished *** Terminating app due to uncaught exception 'System.InvalidOperationException', reason: 'Window was already destroyed (System.InvalidOperationException) at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Destroying() at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_1(UIApplication app) at Microsoft.Maui.MauiUIApplicationDelegate.WillTerminate(UIApplication application) ``` After: ``` [KeyWindow] Scene will invalidate: UIWindowScene ... [40438] SceneDidDisconnect handlers finished [40438] WillTerminate handlers finished ``` `xcrun simctl terminate` kills the process without calling `applicationWillTerminate`, so it doesn't reproduce this. Not checked: Mac Catalyst (no local workload) and a physical device. ### Issues Fixed Fixes dotnet#40438 --------- Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…hread - Prefer the iPhone 11 Pro profile XHarness used while the selected runtime supports it. Shell flyout header geometry tests (#39877) fail on Dynamic Island phones; this keeps device coverage equivalent to Helix. - WillTerminateDestroysMainPageWindowOnce (#40452) constructed the Application off the UI thread; native MTP test bodies run on the thread pool, so resolve it through InvokeOnMainThreadAsync. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Description of Change
An iOS app that sets
MainPageand has a scene manifest crashes withInvalidOperationException: Window was already destroyedevery time the user swipes it away in the app switcher.When the scene disconnects,
SceneDidDisconnectcallsDestroying()on the window.Application.RemoveWindowreturns early for theMainPagewindow, so that window, already destroyed, stays inApplication.Windows. UIKit then callsapplicationWillTerminate, and theWillTerminatehandler callsapp.GetWindow()?.Destroying()on it a second time. Its comment assumesGetWindow()is null by then, which isn't true forMainPageapps.WillTerminatenow skips a window whose handler is already disconnected, which is whatDestroying()leaves behind. If terminate arrives without a prior scene disconnect, the window is still destroyed. I chose this over the!HasSceneManifest()guard the neighbouring handlers use because it depends on the window's state, not on Info.plist.#24191 was closed with "use CreateWindow", but the scene manifest is now in the .NET 11 templates and required by the iOS 27 SDK, so every app that still uses
MainPagehits this crash on force-close.Tests
WindowTests.WillTerminateDestroysMainPageWindowOnce(Controls device test, Window category, iOS and Mac Catalyst) setsMainPage, connects the window, optionally callsDestroying()asSceneDidDisconnectdoes, then runs the registeredWillTerminatehandlers. It checks that nothing throws and thatDestroyingruns exactly once in both cases.On the iPhone 17 simulator (iOS 26.5), Window category: 53/57 without the fix (the 4 failures are the scene-disconnect cases, with the same stack as the issue), 57/57 with it. Application 1/1 and Lifecycle 9/9 with the fix.
Before / after (sandbox app with
MainPageset, Release build, swiped away in the app switcher with Appium; the app logs a line after MAUI's own handlers finish). There's no video because the app disappears either way.Before:
After:
xcrun simctl terminatekills the process without callingapplicationWillTerminate, so it doesn't reproduce this. Not checked: Mac Catalyst (no local workload) and a physical device.Issues Fixed
Fixes #40438