Repository navigation
[iOS] Fix DragEventArgs.GetPosition NullReferenceException in DragOver/DragLeave - #40441
Merged
Merged
Conversation
DragOver and DragLeave event args reference the local drag session only weakly. UIKit returns a transient managed wrapper for LocalDragSession, so a GC while a handler is running can collect it, and DragEventArgs.GetPosition then throws a NullReferenceException from CalculatePosition. This crashed the iOS CoreCLR DropEventCoordinates UI test. Keep the session alive until the handlers have run. CalculatePosition now returns null when there is no session or it has already been collected. DragOver also returns null when there is no local drag session, matching DragLeave and Drop. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
kubaflo
temporarily deployed
to
copilot-pat-pool
November 19, 2026 18:23 — with
GitHub Actions
Inactive
Contributor
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 40441Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 40441" |
This comment has been minimized.
This comment has been minimized.
kubaflo
temporarily deployed
to
copilot-pat-pool
November 19, 2026 18:24 — with
GitHub Actions
Inactive
1 task
kubaflo
temporarily deployed
to
copilot-pat-pool
November 19, 2026 18:25 — with
GitHub Actions
Inactive
kubaflo
temporarily deployed
to
copilot-pat-pool
November 19, 2026 18:27 — with
GitHub Actions
Inactive
kubaflo
temporarily deployed
to
copilot-pat-pool
November 19, 2026 18:28 — with
GitHub Actions
Inactive
kubaflo
temporarily deployed
to
copilot-pat-pool
November 19, 2026 18:29 — with
GitHub Actions
Inactive
4 tasks
SubhikshaSf4851
pushed a commit
to SubhikshaSf4851/maui
that referenced
this pull request
Nov 23, 2026
…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>
kubaflo
added a commit
to kubaflo/maui
that referenced
this pull request
Nov 23, 2026
…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>
SuthiYuvaraj
pushed a commit
to SuthiYuvaraj/maui
that referenced
this pull request
Nov 25, 2026
…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>
KarthikRajaKalaimani
pushed a commit
to KarthikRajaKalaimani/maui
that referenced
this pull request
Nov 26, 2026
…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>
This branch was previously deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 from this PR and let us know in a comment if this change resolves your issue. Thank you!
Target Branch
mainnet11, for reviewFor .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 aNullReferenceExceptioninside aDragOverorDragLeavehandler. The exception crashes the app.CI evidence. The
DropEventCoordinatesUI test crashed the HostApp on both attempts on the iOS CoreCLR lane of SR2 candidate #40116 (maui-pr-uitests 1690394, iOS latest Editor..Fr job). The job's.ipsreports show SIGABRT fromxamarin_process_managed_exceptionunder-[UIDropInteraction _dragDestinationGestureStateChanged:]. The logarchive shows the managed exception:Root cause.
SessionDidUpdateandSessionDidExitpasssession.LocalDragSession, a transient managed wrapper of the native local drag session, intoHandleDragOverandHandleDragLeave.WeakReferenceto it in theGetPositioncallback, and nothing else on the path keeps it alive:PlatformDragEventArgsholds the outerIUIDropSession, not the local drag session.CalculatePositionthen callsLocationInViewon a null session.HandleDragOverbuilt a weak reference even whenLocalDragSessionis null (a drag from another app), soGetPositionalways threw in that case.HandleDragLeaveandHandleDropalready return null there.The weak references date from 2023, and the code is identical on
net11, the candidate andmain. This is a latent bug that the CoreCLR lane's GC timing exposed, not a candidate regression.Fix (
DragAndDropDelegate.cs, iOS/Mac Catalyst):HandleDragOverandHandleDragLeavecallGC.KeepAlive(session)after dispatching to the recognizers. The session stays alive while handlers can synchronously callGetPosition, and the event args still don't hold it strongly.HandleDragOveruses a nullsessionRefwhen there is no local drag session, matchingHandleDragLeaveandHandleDrop.CalculatePositionreturnsnullwhen the session reference is missing or its target has already been collected, instead of dereferencing null. This covers aGetPositioncall made after the handler returns.Tests (
DragAndDropTests.iOS.cs,Gesturecategory):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 callsGetPosition, and the test asserts the fake session's location.DragOverPositionIsNullWithoutLocalDragSession: with no local drag session,GetPositioninDragOverreturnsnull.Validation (iOS 18.5 simulator, Release, CoreCLR, the same runtime as the failing CI lane):
pwsh .github/skills/run-device-tests/scripts/Run-DeviceTests.ps1 -Project Controls -Platform ios -TestFilter "Category=Gesture" -DeviceUdid <iOS 18.5 sim> -SkipXcodeVersionCheckwith only the new tests (no fix)NullReferenceExceptionatDragAndDropDelegate.CalculatePosition, the same frame as the CI crash.dotnet format whitespace --folder --include <changed files> --verify-no-changesThe 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 #40116 CI. Searched for related issues and PRs and found no duplicate. #28416/#28417 is an unrelated Mac Catalyst
DragAndDropDelegateNRE.