Files
magpie/internal/gui/webviewmend_app.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

139 lines
3.9 KiB
Go

//go:build !nogui
package gui
import (
"log"
"sync"
"time"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
// mendWebView has w's WebView2 failures handled (webviewmend.go) once its
// first page has come, by then its controller is made. Off Windows there
// is no WebView2 and onProcessFailed does nothing.
func (h *host) mendWebView(w *application.WebviewWindow) {
var once sync.Once
var mu sync.Mutex
var last time.Time // the last reload or remake
w.OnWindowEvent(events.Windows.WebViewNavigationCompleted, func(*application.WindowEvent) {
once.Do(func() {
application.InvokeAsync(func() {
err := onProcessFailed(w, func(kind uint32) {
what, m := webViewMendFor(kind)
mu.Lock()
now := time.Now()
ok := mendAllowed(m, last, now)
if ok && (m == mendReload || m == mendRemake) {
last = now
}
mu.Unlock()
if !ok {
log.Printf("webview (%s window): %s again within %s; left as it is", w.Name(), what, mendGap)
return
}
log.Printf("webview (%s window): %s; %s", w.Name(), what, mendWords[m])
// out of WebView2's own callback
application.InvokeAsync(func() { h.mend(w, m) })
})
if err != nil {
log.Printf("webview (%s window): its process failures go unhandled: %v", w.Name(), err)
}
})
})
})
}
// mend does m for w; on the main thread.
func (h *host) mend(w *application.WebviewWindow, m webViewMend) {
switch m {
case mendRedraw:
redrawShown(w)
case mendReload:
if err := reloadWebView(w); err != nil {
log.Printf("webview (%s window): reload: %v", w.Name(), err)
}
case mendRemake:
go h.remakeWindow(w)
}
}
// redrawShown hides w's controller and shows it again, if w is open: what
// closing it to the tray and opening it again does, without the window
// going. On the main thread.
func redrawShown(w *application.WebviewWindow) {
if !windowOpen(w) {
return // hidden or minimised: its showing draws it anew
}
if err := webViewVisible(w, false); err != nil {
log.Printf("webview (%s window): hide: %v", w.Name(), err)
return
}
// a moment, so the hide is taken as one and not folded into the show
time.AfterFunc(100*time.Millisecond, func() {
application.InvokeAsync(func() {
if !windowOpen(w) {
return // closed or minimised meanwhile: Wails shows it when it opens
}
if err := webViewVisible(w, true); err != nil {
log.Printf("webview (%s window): show: %v", w.Name(), err)
}
})
})
}
// remakeWindow lets w go, as lightweight mode does, and makes the main
// window again at once if it was open; the panel is made again on its
// next open. Not on the main thread (winMu).
func (h *host) remakeWindow(w *application.WebviewWindow) {
var open, main bool
h.winMu.Lock()
application.InvokeSync(func() {
switch w {
case h.main:
main, open = true, windowUp(w)
h.main = nil
h.gone.Store(w) // its closing hook lets it close
case h.panel:
h.panel = nil
if h.tray != nil {
h.tray.AttachWindow(nil) // the untyped nil: no window
}
default:
w = nil // let go already
}
})
h.winMu.Unlock()
if w == nil {
return
}
w.Close()
if main && open {
// a new webview made while the old browser process is still
// going down fails, and Wails ends magpie for it
awaitBrowserGone()
application.InvokeSync(func() {
if h.main == nil {
h.openMain("")
}
})
}
}
// mendOnWake draws the open windows again after the computer wakes: a GPU
// that lost its device while asleep can leave WebView2's surface white.
func (h *host) mendOnWake() {
h.app.Event.OnApplicationEvent(events.Common.SystemDidWake, func(*application.ApplicationEvent) {
application.InvokeAsync(func() {
for _, w := range []*application.WebviewWindow{h.main, h.panel} {
if w != nil && windowOpen(w) {
log.Printf("webview (%s window): the computer woke; drawing it again", w.Name())
redrawShown(w)
}
}
})
})
}