Skip to content

[net11.0][Android] Fix HybridWebView disconnect container cleanup - #39570

Closed
kubaflo wants to merge 1 commit into
net11.0from
kubaflo-net11-hybrid-disconnect-fix
Closed

kubaflo wants to merge 1 commit into
net11.0from
kubaflo-net11-hybrid-disconnect-fix

Conversation

@kubaflo

@kubaflo kubaflo commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

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!

Description of Change

Fix the Android HybridWebViewTests_Initialization.DisconnectHandlerRemovesClippingContainer baseline failure on net11.0, independently of the GC fixture change in #39536.

ElementHandler now calls OnDisconnecting() before clearing PlatformView. The generic ViewHandler.OnDisconnecting() removes the container while the platform view is still available. Normal Android unwrapping moves that platform view into the wrapper's old parent. By the time the HybridWebView-specific typed DisconnectHandler runs, ContainerView is already null, so its late detachment cannot prevent the old WebView from remaining in the outer FrameLayout.

Detach the HybridWebView wrapper in a private-protected OnDisconnecting() override, before calling the base implementation. Base cleanup can then empty the detached wrapper and reset HasContainer/ContainerView without reparenting the old WebView into the host. Keep client disconnection and StopLoading() in typed native cleanup.

This is intentionally Android/HybridWebView-only:

  • Keep the existing protected RemoveContainer override and public API intact.
  • Preserve [Android] Fix off-screen WebView crash without changing generic view handlers #38467's required native clipping wrapper, ClipChildren=true, and absence of explicit WebView ClipBounds.
  • Do not change generic ViewHandler, WrapperView, Windows wrapper policy, tests, assertions, timing, dependencies, or pipelines.
  • Preserve fresh native-view/wrapper creation on reconnect and repeated-disconnect cleanup.

The failure is present in both Android Mono and CoreCLR in the original #39536 device build 1644557 and exact-base build 1643729, with Assert.Null() receiving an Android FrameLayout. It is not caused by the GC fixture change.

Native causal validation

Local environment: repository-pinned .NET SDK 11.0.100-rtm.26514.118 (product version 11.0.100), Android workload 37.2.15, Android API 33 ARM64, Mono, private headless emulator emulator-5708, native WebView 109.0.5414.123. Base: 7b0ea7800db3f3faccbaecb665d32b7c61eaf932.

Executed the unchanged six-case initialization class, not a proxy reproduction. Each repeat starts a fresh app process and uses a unique device/result XML filename. The temporary module initializer supplied by the run-device-tests skill restricts XHarness to Microsoft.Maui.DeviceTests.HybridWebViewTests_Initialization; it is not committed.

Product state Repeats Actual XML result per process
Unmodified base 3/3 red 5 passed, disconnect test failed with Assert.Null / actual FrameLayout
Fix applied 3/3 green 6 passed, 0 failed/skipped/errors
Product fix removed, rebuilt 3/3 red Same intended disconnect failure; 5 siblings passed
Fix restored, rebuilt 3/3 green 6 passed, 0 failed/skipped/errors

The existing regression case checks wrapper/native detachment, an empty old wrapper, cleared handler container state, fresh wrapper/native instances and clipping after reconnect, and repeated disconnect. The five siblings exercise initialization callbacks and native settings; every sibling passed in every causal arm. One preliminary baseline execution also reproduced the same failure; the table reports the three fully recorded repeats.

The broader TestFilter=Category=HybridWebView run on the restored fix also passed: 101 total / 96 passed / 5 existing skips / 0 failures / 0 errors. Actual XML contains 99 HybridWebView cases (94 passed, 5 skipped), plus two uncategorized AlertDialogTests cases included by the existing category runner (both passed). It covers JavaScript evaluation/invocation, .NET dispatch, message origins, raw messages, content-root resolution, request interception, initialization, and disconnect/reconnect. No filter, skip, or assertion in the repository was changed.

Commands and observed results ($E is the untracked session evidence directory; $APK is the arm-specific copy of the built signed Controls APK):

dotnet build Microsoft.Maui.BuildTasks.slnf -c Release \
  -p:BuildTaskOnlyBuild=true -p:IncludeAndroidTargetFrameworks=true \
  -p:IncludeIosTargetFrameworks=false -p:IncludeMacCatalystTargetFrameworks=false \
  -p:IncludeMacOSTargetFrameworks=false -p:TreatWarningsAsErrors=false \
  -bl:"$E/buildtasks.binlog" --nologo -v:q
# Passed: 0 errors, 3 existing Gradle warnings.

dotnet build src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj \
  -t:Rebuild -c Release -f net11.0-android -r android-arm64 \
  -p:IncludeIosTargetFrameworks=false -p:IncludeMacCatalystTargetFrameworks=false \
  -p:IncludeMacOSTargetFrameworks=false -p:IncludeWindowsTargetFrameworks=false \
  -p:IncludeTizenTargetFrameworks=false -p:TreatWarningsAsErrors=false \
  -p:UseMonoRuntime=true -p:_DisableCheckForUnsupportedMonoMobileRuntime=true \
  -p:RunAOTCompilation=false -p:EmbedAssembliesIntoApk=true \
  -p:AndroidPackageFormat=apk \
  -p:CustomAfterMicrosoftCSharpTargets="$E/MauiCopilotClassFilter.targets" \
  -bl:"$E/$arm-build.binlog" --nologo -v:q
# Fixed, reverted, restored: all passed; 0 errors, 6 existing warnings each.
# Initial baseline used the same properties without -t:Rebuild:
# passed; 0 errors, 3 existing warnings.

adb -s emulator-5708 shell am force-stop com.microsoft.maui.controls.devicetests
dotnet xharness android test --app "$APK" \
  --package-name com.microsoft.maui.controls.devicetests \
  --device-id emulator-5708 --output-directory "$E/$arm-$repeat" \
  --timeout 00:05:00 --arg TestFilter=Category=HybridWebView \
  --arg "results-file-name=hybrid-$arm-$repeat.xml"
# Actual test XML results are shown in the causal table above.

dotnet xharness android test \
  --app artifacts/bin/Controls.DeviceTests/Release/net11.0-android/android-arm64/com.microsoft.maui.controls.devicetests-Signed.apk \
  --package-name com.microsoft.maui.controls.devicetests \
  --device-id emulator-5708 --output-directory "$E/hybrid-category" \
  --timeout 00:10:00 --arg TestFilter=Category=HybridWebView \
  --arg results-file-name=hybrid-category.xml
# Passed: 96 passed, 5 existing skipped, 0 failed/errors.
# Rebuilt with the same command above after clearing only the temporary class filter.

dotnet format Microsoft.Maui.sln --no-restore --exclude Templates/src \
  --exclude-diagnostics CA1822 \
  --include src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Android.cs
# Passed (workspace-loading warnings); no unrelated files changed.

Local causal validation is Mono-only; no local CoreCLR, Apple, Windows, desktop UI, or RenderThread overscroll-crash validation is claimed. An idle emulator-backend abort occurred between completed fixed runs and the reverted run; only the task-owned AVD was restarted. Reverted and restored evidence comes from subsequent fresh processes and newly written XML, not reused results. The task-owned AVD has been stopped and removed.

Open and recently merged PRs were checked for HybridWebView disconnect/container cleanup, OnDisconnecting, and this Android handler path; no equivalent fix was found. #38467 is preserved, not replaced or duplicated.

Issues Fixed

Repairs the independent Android HybridWebView baseline failure surfaced during #39536 validation. Existing regression coverage: #38467. This does not close or modify #39536.

Intentionally targets net11.0: this adapts HybridWebView teardown to that branch's pre-disconnect container lifecycle. No main, release, or GC fixture changes are included.

Detach the HybridWebView wrapper before base OnDisconnecting unwraps its native view. Keep native client cleanup in typed disconnect and preserve existing clipping and protected container API.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 35de8bc6-f5d1-4ff4-9cd2-e38b15c785b0
Copilot AI balanced review requested due to automatic review settings October 22, 2026 18:21
@kubaflo
kubaflo temporarily deployed to copilot-pat-pool October 22, 2026 18:21 — with GitHub Actions Inactive
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 39570

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 39570"

@kubaflo
kubaflo temporarily deployed to copilot-pat-pool October 22, 2026 18:21 — with GitHub Actions Inactive
@azure-pipelines

This comment has been minimized.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The native lifecycle change needs human confirmation because current CI and reported Android execution results were not independently verified.

No open issues 🎉

What changed in this PR

This PR adapts Android HybridWebView teardown to the net11.0 pre-disconnect lifecycle, preventing the old native WebView from remaining in its host container. The title and description match the supplied diff.

Changes:

  • Detach the wrapper in OnDisconnecting() before base container cleanup can reparent the WebView.
  • Remove late container cleanup from typed DisconnectHandler, retaining client disconnection and StopLoading().
File Description
src/​Core/​src/​Handlers/​HybridWebView/​HybridWebViewHandler.Android.cs Moves wrapper detachment earlier and clarifies the existing container-cleanup comment.

Review effort: Balanced

@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@github-actions github-actions Bot added s/agent-review-in-progress AI review is currently running for this PR and removed s/agent-review-in-progress AI review is currently running for this PR labels Oct 23, 2026
@MauiBot

This comment has been minimized.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Oct 23, 2026
@kubaflo

kubaflo commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

/review expert-review

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Oct 23, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 0 findings

Reviewed commit ac8870b.

@MauiBot

MauiBot commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@kubaflo — existing-test verification for commit ac8870bc.

Result SKIPPED Platform Android Commit ac8870bc


🧪 Gate analysis — click to expand

⚠ Skipped. No runnable PR tests were selected. The fix has not been verified by this gate.

No interpretable test-group summary was produced. No additional test execution is inferred.

PR head: ac8870bc.

Without-fix baseline: 4d29977f.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — ac8870bc Deployed Oct 22, 2026 by kubaflo via conclusion #2469
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants