Skip to content

[Windows] Guard HybridWebView init against a closed CoreWebView2 - #40771

Merged
kubaflo merged 1 commit into
net11from
fix-hybridwebview-windows-init-null
Nov 29, 2026
Merged

kubaflo merged 1 commit into
net11from
fix-hybridwebview-windows-init-null

Conversation

@kubaflo

@kubaflo kubaflo commented Nov 28, 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!

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: cdcaf4125975a7ea1c81ca95eeca4e2ef9018f02

Dimension Assessment Rationale
Change confidence 6/10 ↑ Minimal null guard, cannot regress the non-null path.
↓ Not run: Windows-only, no Windows host available locally.
Diagnosis confidence 7/10 ↑ CI log (maui-pr-uitests 1703818, Windows controls unpackaged) shows NRE at HybridWebViewHandler.Windows.cs:339 right after EnsureCoreWebView2Async.
↓ Crash may also have other causes; no causal red/green run.
Evidence fit 7/10 ↑ Stack trace matches the dereference of CoreWebView2 after init.
↓ NavigationPage and Shell failures are not explained by this change.

Target Branch

  • I selected the applicable base branch (net11, .NET 11 fix, for review).

Description of Change

TryInitializeWebView2 dereferenced webView.CoreWebView2 after EnsureCoreWebView2Async. If the control is closed while initialization is in flight, CoreWebView2 is null and the continuation throws a NullReferenceException on the dispatcher, crashing the test host (HybridWebView category, exit 7 in maui-pr-uitests 1703818). Return false instead when it is null, matching the existing null handling in Disconnect.

Validation: not run locally (Windows-only code); relies on CI.

Issues Fixed

Fixes CI failure in HybridWebView Windows device tests on net11.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@kubaflo

kubaflo commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@dalexsoto stamp

@kubaflo
kubaflo temporarily deployed to copilot-pat-pool November 28, 2026 20:29 — with GitHub Actions Inactive
@github-actions

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@kubaflo

kubaflo commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@dalexsoto stamp

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Stamping as requested by @kubaflo; no code review performed.

@kubaflo
kubaflo merged commit 839a945 into net11 Nov 29, 2026
47 of 48 checks passed
@kubaflo
kubaflo deleted the fix-hybridwebview-windows-init-null branch November 29, 2026 09:20
kubaflo added a commit that referenced this pull request Nov 29, 2026
)

<!-- 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!

<!-- copilot-assessment:v1 -->
### Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: `86ff3df460c9b65517193fd7aac96e3778be24e2`

| Dimension | Assessment | Rationale |
|---|---|---|
| Change confidence | 7/10 | ↑ Minimal null guard on a path that
currently throws; the non-null path is unchanged.<br>↑ Same change as
#40771, merged to net11.<br>↓ Windows-only; not run locally (macOS
host), CI is the validation. |
| Diagnosis confidence | 8/10 | ↑ main's maui-pr-uitests build 1703917
shows the same NullReferenceException at
HybridWebViewHandler.Windows.cs:339 in TryInitializeWebView2
(HybridWebView category, exit 7), the line right after
EnsureCoreWebView2Async.<br>↓ Why CoreWebView2 is null (control closed
during init) is inferred from the code, not observed. |
| Evidence fit | 7/10 | ↑ Exact stack frame matches the guarded
dereference.<br>↓ Not confirmed until maui-pr-uitests runs on this PR. |
<!-- /copilot-assessment -->

### Target Branch

- [x] I selected the applicable base branch (`main`, .NET 12 fix).

### Description of Change

`TryInitializeWebView2` dereferenced `webView.CoreWebView2` after
`EnsureCoreWebView2Async`. If the control is closed while initialization
is in flight, `CoreWebView2` is null and the continuation throws a
`NullReferenceException` on the dispatcher, which crashes the Windows
device-test host (HybridWebView category, exit 7; maui-pr-uitests build
1703917 on `main`). Return `false` instead when it is null, matching the
existing null handling in `Disconnect`.

Same fix as #40771, which merged to `net11`.

Validation: not run locally (Windows-only code); relies on CI.

### Issues Fixed

Fixes the HybridWebView Windows device-test host crash on `main`.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 29, 2026
…evice-test waits (#40818)

<!-- copilot-assessment:v1 -->
### Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: `3eec8791098beea08fbb2e411202fb58d7d1c435`

| Dimension | Assessment | Rationale |
|---|---|---|
| Change confidence | 8/10 | ↑ Final-head device-test build 1704477
succeeded; HybridWebView passed in both Windows modes (104 passed, 4
skipped each). All three regression tests executed in the unpackaged
log. The existing polling harness passes 3/3 locally.<br>↓ Two
successful runs per mode cannot eliminate an intermittent native
initialization failure. |
| Diagnosis confidence | 7/10 | ↑ WinUI catches controller-creation
errors and reports them through CoreWebView2Initialized, so
EnsureCoreWebView2Async can complete with a null core; the old
false-result path drops JavaScript requests.<br>↓ Build 1704369 did not
record the native error; teardown/shared-environment causality is not
established. |
| Evidence fit | 8/10 | ↑ Raw logs in builds 1704453 and 1704477 confirm
four passing Windows HybridWebView category executions, including the
previously stalled Invoke_NoParam_ReturnTaskComplex and focused
failure-path tests.<br>↓ This is bounded-failure protection and improved
diagnostics, not proof that the underlying native failure has been
eliminated. |
<!-- /copilot-assessment -->

<!-- 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

<!-- Enter description of the fix in this section -->

Follow-up to #40796, not a revert. Keep the null-core guard, but
distinguish a disconnected handler from an initialization failure:

- Capture the native error from `CoreWebView2Initialized`, fault the
ready task, and retain the error as the inner exception.
- Propagate failed initialization (including a false ready result) to
`EvaluateJavaScriptAsync` instead of abandoning its completion source.
- Log failures in fire-and-forget navigation/raw-message operations
without an unhandled dispatcher exception.
- Do not navigate or subscribe events for a disconnected or replaced
platform view after asynchronous initialization.
- Bound HybridWebView page/status polling, including an individual
JavaScript evaluation that never completes. Bound test operations
separately.
- Add three focused regression cases for faulted initialization, false
initialization, and a stalled polling evaluation. Adapt the existing
source-polling Pester harness to the helper's internal visibility and
cancellation-token dependency.

**Evidence:** [build
1704369](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1704369),
raw log 836, Windows controls unpackaged. Last `TEST_START` at 18:05:48Z
is `Invoke_NoParam_ReturnTaskComplex`; no further test progress precedes
the task timeout at 19:47:16Z. Packaged passed in that build; the six
preceding supplied builds passed HybridWebView in both Windows jobs.
This is one occurrence, not proof that #40796 caused the underlying
native failure.

**Why a null core does not prove teardown:** [WinUI
WebView2.cpp](https://github.com/microsoft/microsoft-ui-xaml/blob/main/controls/dev/WebView2/WebView2.cpp),
`CreateCoreWebViewFromEnvironment`, catches `hresult_error`, fires
`CoreWebView2Initialized(hr)`, and completes its coroutine.
`EnsureCoreWebView2Async` awaits that operation. No native HRESULT was
recorded in the failing run, so this PR exposes it rather than guessing
at an environment workaround or adding blind retries.

**Duplicate check:** searched open/recent PRs for HybridWebView Windows,
EnsureCoreWebView2Async, and CoreWebView2 null. #40796/#40771 add the
valid null guard; #38569 handles regular WebView; #36742 fixes a
memory-test initialization wait. No open duplicate addressing
HybridWebView failure propagation and bounded waits was found.

**Validation**

- `git diff --check` — passed.
- `dotnet format Microsoft.Maui.sln --no-restore --include
src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs
src/Core/src/Platform/Windows/MauiHybridWebView.cs
src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTestsBase.cs
src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests_EvaluateJavaScriptAsync.cs
src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests_EvaluateJavaScriptAsync.Windows.cs
--exclude Templates/src --exclude-diagnostics CA1822 --verbosity quiet`
— completed successfully; formatting, not Windows compilation.
- `pwsh -NoProfile -Command '$c = New-PesterConfiguration; $c.Run.Path =
"./eng/devices/Direct-TestDevice.Tests.ps1"; $c.Filter.FullName =
"HybridWebView startup polling budget*"; $c.Run.Exit = $true;
$c.Output.Verbosity = "Detailed"; Invoke-Pester -Configuration $c'` —
passed, 3/3.
- Windows tests are not run locally (macOS host). Requested `/azp run
maui-pr-devicetests`.
- **[Final-head build
1704477](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1704477)
succeeded.** Its tested merge `b5b01d2dc0a35d5763f3e108f320ebfc0ce7e0ad`
has PR head `3eec8791098beea08fbb2e411202fb58d7d1c435` as a parent. Both
Windows controls jobs succeeded. Packaged raw log 777 and unpackaged raw
log 747 each report **HybridWebView 104 succeeded / 0 failed / 4
skipped**. Unpackaged logs show the three new tests at 20:58:15–17Z and
`Invoke_NoParam_ReturnTaskComplex` at 20:59:08Z, followed by the
successful summary at 20:59:53Z. Packaged completes the category at
21:01:07Z.
- [Build
1704453](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1704453),
product/test revision `49ef2ab01b92e20a7b6115a87323241761d04e4f`:
packaged raw log 747 and unpackaged raw log 771 each report
**HybridWebView 104 succeeded / 0 failed / 4 skipped**. The entire build
failed because unpackaged Entry failed `UnFocusOnEntryAfterPagePop`
(`EntryTests.cs:186`); no Entry code was changed. Entry passes in the
final-head build.
- First admission 1704451 was canceled before execution. Each requested
run needed a second `/azp run maui-pr-devicetests` comment before
admission.
- Full Pester on the first revision failed: three source-polling tests
were directly affected by the extraction signature and are now
repaired/locally validated. Other reported release/pipeline/model-policy
failures concern files outside this diff. The overall PR still has a
failing Pester check; no claim that every required check is green.

Two successful runs in each Windows mode are useful evidence, not proof
against a rare recurrence. The underlying native initialization error
remains unknown.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Relates to #40796. Addresses the Windows HybridWebView device-test hang
in build 1704369; no separate issue has been filed.

### 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.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

This branch was previously deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants