Repository navigation
fix(webview2): guard Focus() until controller initialisation completes - #5568
Conversation
The WndProc can dispatch WM_SETFOCUS re-entrantly while CreateCoreWebView2ControllerCompleted is still configuring the controller: e.controller is assigned partway through the callback, but inited only flips after all COM setup succeeds. In that window GetController() != nil checks at call sites pass, yet MoveFocus fails with E_INVALIDARG and errorCallback exits the process. With no controller at all, Focus() dereferenced nil. Add IsReady() (atomic load of inited) and make Focus() a no-op until it reports true. Dropping a focus request during startup is harmless; the window manager re-delivers focus once the user interacts with the window. Fixes #5446
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughChromium gains an ChangesFocus readiness guard and initialization safety
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR fixes a Windows WebView2 startup crash caused by Focus() being called re-entrantly (e.g., via WM_SETFOCUS) while the WebView2 controller exists but has not finished COM setup. It does this by introducing a readiness check based on the existing inited atomic flag and making Focus() a safe no-op until initialization is fully complete.
Changes:
- Add
(*Chromium).IsReady()which reports full controller initialization by atomically readinginited. - Guard
(*Chromium).Focus()so it returns early unlessIsReady()is true, preventing calls into a partially configured controller. - Add Windows-only tests to assert
Focus()is safe before readiness, including a concurrent stress test.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| webview2/pkg/edge/chromium.go | Adds IsReady() and uses it to guard Focus() until controller initialization is fully complete. |
| webview2/pkg/edge/chromium_focus_test.go | Adds regression tests for calling Focus() before controller readiness (single-threaded + concurrent). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
MoveFocus can fail after initialisation too: E_INVALIDARG when the window is hidden or minimised to the tray (#4158 reproduces this 100% on tray-click restore). Routing that error through errorCallback exits the process. Log it instead — a failed focus request is never worth killing the application.
|
Scope update: while triaging #4158 (same crash signature, reported a year earlier) a commenter provided a 100% reproducible variant: minimize to tray → click the tray icon → instant crash. That path fails after initialisation — Added one commit: |
ExecuteScript fails transiently while the browser process reconfigures, e.g. RESOURCE_NOT_IN_CORRECT_STATE during a DPI transition when the window is dragged between mixed-DPI monitors (#5544 verification). Script execution is fire-and-forget: dropping a script during a transition is recoverable, exiting the process is not. Log instead, matching the Focus change.
|
Scope addition from #5544's verification: dragging a window between mixed-DPI monitors exposed that |
The resync added a w.chromium.IsReady() guard, but IsReady() exists only in unreleased webview2 source (added in #5568) — it is not in the webview2 module version v3 pins (v1.0.24), so it fails to cross-compile and would break end-user builds. The guard was defense-in-depth only; the real #5605 protection is the DPI-change gate, and the existing GetController()==nil check still covers the unavailable-controller case. Re-add the IsReady guard once a webview2 release including it lands and v3's go.mod is bumped.
) * ci(webview2): auto-release on webview2 source changes to master Add a push trigger (branches: master, paths: webview2/**) to the existing Release webview2 workflow so any change to the webview2 module cuts a new patch tag automatically, then the existing bump-webview2-v3 opens the v3 go.mod bump PR. Prevents the gap where webview2 source gains an API (e.g. IsReady() in #5568) that v3 cannot use because no release was cut (see #5616). Loop-safe: the release pushes a tag (not a master commit) and the bump only edits v3/**, so neither re-triggers this workflow. Adds a concurrency group to serialise near-simultaneous releases. * fix(ci): strip leading v in webview2 auto-increment to avoid vv tag The auto-increment path derived MAJOR from "v1.0.24" as "v1" and then prefixed another v, producing webview2/vv1.0.25. Stripping the leading v (webview2/v prefix) yields the correct webview2/v1.0.25. This path becomes the default once the push trigger fires, so it must be correct.
Description
Fixes the production startup crash from #5446 (successor to the partial #4835 fix).
CreateCoreWebView2ControllerCompletedassignse.controllerpartway through the callback (chromium.go:338) but only flips theinitedatomic after all COM setup succeeds (line 389). The Windows message pump runs during that window, so the WndProc can dispatchWM_SETFOCUSre-entrantly and reachChromium.Focus()while the controller is non-nil but partially configured. The v3-sideGetController() != nilguards (added for #4835) pass,MoveFocusfails withE_INVALIDARG("The parameter is incorrect"), anderrorCallbackexits the process. With no controller at all,Focus()dereferenced nil.Fix
Chromium.IsReady()— atomic load ofinited, the flag that already means "fully initialised".Focus()becomes a no-op untilIsReady(). Dropping a focus request during startup is harmless (the window manager re-delivers focus on interaction); calling into a partially-initialised controller is fatal.Guarding inside
Focus()makes every call site safe — including the four TOCTOU-shapedGetController() != nil → Focus()patterns in v3'swebview_window_windows.go— without v3 code changes. Once this ships in a webview2 release, the existingbump-webview2-v3automation picks it up; the v3 call sites can optionally migrate toIsReady()afterwards as a cleanup.Verification (win-node1, windows/amd64, go1.26.2)
chromium.gocrashes withMoveFocus(0x0, …)nil-pointer panic — the reported failure mode.TestFocusBeforeControllerReadyandTestFocusBeforeControllerReadyConcurrent(16 goroutines × 1000 iterations) pass; fullpkg/edgesuite passes;go vetclean for the changed file (remaining warnings are pre-existing COM interop patterns).Fixes #5446
Type of change
Checklist:
Summary by CodeRabbit
Bug Fixes
Tests