Files
magpie/internal/gui/webviewmend_windows.go
T
yetone f46a37f509 gui: a WebView2 process that fails is logged and the window comes back by itself
aquaarboraurum on X (magpie 0.1.4040, Windows 11, WebView2 138): the
main window went white after a while, leaving the title bar, a strip at
the left edge and the status bar. Closing it to the tray and opening it
again brought it back, and the log said nothing.

Wails registers for WebView2's ProcessFailed but leaves its
ProcessFailedCallback unset, so nothing in magpie heard any WebView2
process end or fail. Now each window's failures are handled, once its
first page has come (webviewmend*.go):

- each one is logged: "webview (main window): GPU process exited;
  drawing it again", and so on for every COREWEBVIEW2_PROCESS_FAILED_KIND;
- GPU, unknown or a new kind: the controller is hidden and shown again
  (put_IsVisible), what the tray close and open did, without the window
  going;
- a render process that exits or hangs, or a frame's: the page is
  reloaded (ICoreWebView2::Reload);
- the browser process: the window is let go as lightweight mode does and
  the main window made again, once the old browser has let go of the
  profile folder (made at once, Wails ended magpie on a controller
  timeout);
- utility, sandbox helper and plugin processes: logged only;
- a reload or remake again within 30s is logged and left alone, so a
  failing renderer doesn't loop;
- after the computer wakes (SystemDidWake), each open window is drawn
  again the same way, since a GPU that lost its device while asleep is
  the likeliest way to a white surface with the window's frame left.

The cause of the report is not proven. Ending the GPU process on the
Windows box doesn't leave the old code white (WebView2 makes a new one
and the page keeps drawing), and a dead renderer doesn't match a window
that the tray brings back. The log line is what the next report needs.

Run:
- TestWebViewComesBackFromEachProcessEnd (new) on xiaomi-gpu, Windows 11
  25H2, WebView2 157, in the signed-in session (schtasks /it; it skips
  over ssh, session 0): it ends the test build's own renderer, GPU
  process and browser process and checks the page comes back drawn
  (screen pixels over the client area, white share 0.00) - renderer,
  gpu-process and browser all PASS (16.4s).
- Broken by hand (mendWebView taken out of makeMain): all three red -
  "the page didn't load", "hidden=false, never visible again", "timed
  out waiting until the window is made again".
- Old code, GPU process ended, white share sampled 10s: 0.00 every second
  (not white: the cause above is not reproduced).
- TestWebViewMendForEachFailure, TestMendGapHoldsOnlyReloadAndRemake.
- mica: go vet ./internal/gui (darwin, GOOS=windows), go vet -tags nogui
  (darwin, linux, windows), go test -count=1 ./internal/gui ok 137s.
- mica, go test -count=1 -timeout 45m ./... at f9ad4122b plus this
  change: 98 packages ok, one red, TestNoEagerPackageRegexps on
  davsync's setuptext.go (#6534, red on main, fixed in the next commit).
- xiaomi-gpu, go test ./internal/gui over ssh: 9 reds, the same 9 at
  origin/main f9ad4122b run the same way (#6068, #6059, #5928): %AppData%
  unset over ssh, file locks, Smart App Control; none touch WebView2.

Not tried: a real GPU device loss after sleep or display off, the
reporter's WebView2 138, the panel's remake (the same code path as
lightweight mode's let-go).
2026-11-13 19:45:24 +08:00

135 lines
4.1 KiB
Go

//go:build windows && !nogui
package gui
import (
"errors"
"fmt"
"log"
"math"
"reflect"
"syscall"
"time"
"unsafe"
"github.com/wailsapp/wails/v3/pkg/application"
"golang.org/x/sys/windows"
)
// onProcessFailed has f called with the kind of each failure of w's
// WebView2 processes (COREWEBVIEW2_PROCESS_FAILED_KIND), on the main
// thread. Wails' edge.Chromium registers for ProcessFailed and calls its
// ProcessFailedCallback, which Wails itself leaves unset; it is reached
// through the fields chromiumPart walks. On the main thread.
func onProcessFailed(w *application.WebviewWindow, f func(kind uint32)) error {
c := chromiumStruct(w)
if !c.IsValid() {
return errors.New("no WebView2 found in this Wails")
}
cb := c.FieldByName("ProcessFailedCallback")
if !cb.IsValid() || cb.Kind() != reflect.Func || cb.Type().NumIn() != 2 || cb.Type().NumOut() != 0 ||
cb.Type().In(1).Kind() != reflect.Pointer {
return errors.New("Wails' Chromium has no ProcessFailedCallback of the shape known")
}
if !cb.IsNil() {
return errors.New("Wails handles ProcessFailed itself now")
}
cb.Set(reflect.MakeFunc(cb.Type(), func(in []reflect.Value) []reflect.Value {
kind := uint32(math.MaxUint32)
if a := in[1]; !a.IsNil() {
kind = processFailedKind(a.UnsafePointer())
}
f(kind)
return nil
}))
return nil
}
// processFailedKind is ICoreWebView2ProcessFailedEventArgs'
// get_ProcessFailedKind (slot 3, after IUnknown's), MaxUint32 when it
// fails.
func processFailedKind(args unsafe.Pointer) uint32 {
kind := uint32(math.MaxUint32)
if hr, _, _ := syscall.SyscallN(comMethod(args, 3), uintptr(args), uintptr(unsafe.Pointer(&kind))); int32(hr) < 0 {
return math.MaxUint32
}
return kind
}
// webViewVisible is ICoreWebView2Controller's put_IsVisible (slot 4:
// IUnknown's three, then get_IsVisible), as Wails' Chromium Show and Hide
// call it; on the main thread.
func webViewVisible(w *application.WebviewWindow, visible bool) error {
c := chromiumPart(w, "controller")
if c == nil {
return errors.New("no controller")
}
var v uintptr
if visible {
v = 1
}
if hr, _, _ := syscall.SyscallN(comMethod(c, 4), uintptr(c), v); int32(hr) < 0 {
return fmt.Errorf("put_IsVisible(%t): %w", visible, windows.Errno(hr))
}
return nil
}
// reloadWebView is ICoreWebView2's Reload (slot 31 in ICoreWebView2Vtbl,
// WebView2.h), which loads the page again in a new renderer; on the main
// thread.
func reloadWebView(w *application.WebviewWindow) error {
wv := chromiumPart(w, "webview")
if wv == nil {
return errors.New("no webview")
}
const reload = 31
if hr, _, _ := syscall.SyscallN(comMethod(wv, reload), uintptr(wv)); int32(hr) < 0 {
return fmt.Errorf("Reload: %w", windows.Errno(hr))
}
return nil
}
// chromiumStruct is Wails' edge.Chromium for w, as a value whose fields can
// be set; the zero Value before WebView2 is made or in a Wails without
// that path (chromiumPart's).
func chromiumStruct(w *application.WebviewWindow) reflect.Value {
if w == nil {
return reflect.Value{}
}
v := reflect.ValueOf(w).Elem()
for _, name := range []string{"impl", "chromium"} {
if v.Kind() != reflect.Struct {
return reflect.Value{}
}
v = v.FieldByName(name)
for v.IsValid() && v.Kind() == reflect.Interface {
if v.IsNil() {
return reflect.Value{}
}
v = v.Elem()
}
if !v.IsValid() || v.Kind() != reflect.Pointer || v.IsNil() {
return reflect.Value{}
}
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return reflect.Value{}
}
// reached through unexported fields, v can't be set; the same memory
// taken afresh can
return reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem()
}
// awaitBrowserGone waits, up to 15s, for the browser process that ended to
// have let go of the profile folder, and a moment more for its children: a
// controller asked for before then was refused ("not in the correct
// state") and Wails ended the app.
func awaitBrowserGone() {
dir := webView2Dir()
if !awaitWebView2Folder(func() bool { return webView2Free(dir) }, 15*time.Second, 250*time.Millisecond, time.Sleep) {
log.Printf("webview: the profile folder %s is still held; making the window anyway", dir)
}
time.Sleep(time.Second)
}