Skip to content

fix(windows): prevent crash on WebView2 focus during dev hot-reload - #5315

Merged
leaanthony merged 1 commit into
masterfrom
agent/engineer-windows/3439294a
May 3, 2026
Merged

leaanthony merged 1 commit into
masterfrom
agent/engineer-windows/3439294a

Conversation

@leaanthony

@leaanthony leaanthony commented May 3, 2026 •

Copy link
Copy Markdown
Member

Fixes #4835

Summary

  • Fix 1: Add GetThreadDpiAwarenessContext + AreDpiAwarenessContextsEqual to w32; use them in setupDPIAwareness to reliably detect permonitorv2 manifest-set DPI awareness before calling SetProcessDpiAwarenessContext.
  • Fix 2: Guard all four chromium.Focus() call sites with GetController() != nil. go-webview2 calls os.Exit(1) on any MoveFocus error, so calling Focus when the controller is nil or mid-init is fatal.

Test plan

  • wails3 dev on Windows 10/11: save a .go file, hot-reload must not crash
  • Normal maximise/restore/fullscreen focus still works

?? Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed potential WebView display issues during window state transitions.
    • Enhanced DPI awareness handling on Windows for improved high-resolution display support.

Two related issues during `wails3 dev` hot-reload on Windows:

1. SetProcessDpiAwarenessContext "Access is denied": the old guard used
   GetProcessDpiAwareness (SHCORE) to detect manifest-set DPI awareness,
   but this misses awareness set via SetProcessDpiAwarenessContext.
   Fix: prefer GetThreadDpiAwarenessContext (User32, Win10 1607+) which
   reflects both manifest and API-set contexts; fall back to SHCORE on
   older Windows.

2. [WebView2 Error] The parameter is incorrect / os.Exit(1): all four
   chromium.Focus() call sites in webview_window_windows.go now guard
   with GetController() != nil before calling into go-webview2. This
   prevents the fatal exit when the WebView2 controller is nil or torn
   down mid-lifecycle (e.g. during rapid process restarts in dev mode).

Fixes: #4835

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Co-authored-by: multica-agent <[email protected]>
Copilot AI review requested due to automatic review settings May 3, 2026 11:35
@coderabbitai

coderabbitai Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 44245372-c041-4296-aee9-24a804cb6fdf

📥 Commits

Reviewing files that changed from the base of the PR and between ea7e467 and d55516d.

📒 Files selected for processing (3)
  • v3/pkg/application/application_windows.go
  • v3/pkg/application/webview_window_windows.go
  • v3/pkg/w32/user32.go

Walkthrough

Three modules are improved to prevent crashes during window operations: Windows API bindings for DPI awareness context detection are added, DPI setup now preempts redundant API calls, and WebView2 focus operations are guarded against a nil controller state.

Changes

WebView2 Stability & DPI Awareness

Layer / File(s) Summary
Windows API Bindings
v3/pkg/w32/user32.go
New lazy-proc bindings for GetThreadDpiAwarenessContext and AreDpiAwarenessContextsEqual are added. Wrapper functions HasGetThreadDpiAwarenessContextFunc(), GetThreadDpiAwarenessContext(), HasAreDpiAwarenessContextsEqualFunc(), and AreDpiAwarenessContextsEqual() expose these APIs safely with availability checks.
DPI Awareness Pre-check
v3/pkg/application/application_windows.go
setupDPIAwareness() now preempts redundant DPI setter calls by checking the current thread's DPI awareness context; if already set to UNAWARE-mismatch, it returns early, avoiding "Access is denied" errors. The fallback chain (SetProcessDpiAwarenessContext → SetProcessDpiAwareness → SetProcessDPIAware) remains unchanged.
WebView Focus Guards
v3/pkg/application/webview_window_windows.go
Calls to w.chromium.Focus() in maximise(), restore(), fullscreen(), and the shared focus() method are now guarded with w.chromium.GetController() != nil checks to prevent crashes when the WebView2 controller is invalid or nil.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

Bug, Windows, go, v3, size:M

Poem

🐰 A rabbit hops through DPI lands so bright,
Checking contexts ere setting them right,
WebView's controller now guarded with care,
No more crashes when none should be there!
Hop hop — stability blooms everywhere! 🌿

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description identifies the issue (#4835) and explains both fixes concisely. However, the description template requires a structured checklist (Type of change, How Has This Been Tested, Test Configuration, Checklist) which is mostly missing or incomplete. Complete the description template: select Type of change (Bug fix), fill in How Has This Been Tested section, add Test Configuration (wails doctor output), and check all applicable checklist items.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main fix: preventing WebView2 focus crashes during dev hot-reload on Windows. It matches the primary objective described in PR #4835.
Linked Issues check ✅ Passed The code changes address the primary coding objectives from #4835: (1) guard WebView2 Focus calls with GetController() != nil checks; (2) add DPI awareness context detection APIs and improve SetProcessDpiAwarenessContext handling. Both fixes are implemented.
Out of Scope Changes check ✅ Passed All changes align with the objectives from #4835. The w32 user32.go additions, application_windows.go DPI changes, and webview_window_windows.go Focus guards directly address the identified hot-reload crash issues.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/engineer-windows/3439294a

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.11.4)

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.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Pull request overview

Fixes a Windows-only crash path during wails3 dev hot-reload by making DPI-awareness detection more reliable (avoiding SetProcessDpiAwarenessContext “access denied” when a manifest already set it) and by preventing fatal WebView2 focus calls when the controller is not available.

Changes:

  • Add GetThreadDpiAwarenessContext + AreDpiAwarenessContextsEqual (and feature-detection helpers) to w32 and use them to detect manifest-set DPI awareness more reliably.
  • Update setupDPIAwareness() to prefer thread DPI-awareness context checks before attempting to set process DPI awareness.
  • Guard all chromium.Focus() call sites to avoid calling into go-webview2 when the controller is nil (preventing os.Exit(1)).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
v3/pkg/w32/user32.go Adds missing User32 bindings/helpers for thread DPI-awareness context inspection.
v3/pkg/application/application_windows.go Uses the new DPI-awareness context APIs to skip SetProcessDpiAwarenessContext when already set via manifest.
v3/pkg/application/webview_window_windows.go Adds controller-nil guards around WebView2 focus calls to avoid dev hot-reload crash.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@leaanthony
leaanthony merged commit 464659b into master May 3, 2026
71 checks passed
@leaanthony
leaanthony deleted the agent/engineer-windows/3439294a branch May 3, 2026 12:46
pull Bot pushed a commit to WangNingkai/wails that referenced this pull request May 3, 2026
… prevent crash on WebView2 focus during dev hot-reload
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.

[v3] [WebView2 Error] The parameter is incorrect.

2 participants