Files
magpie/internal/gui/screens_windows.go
T
yetone 18c69aa467 gui: Windows reads the screens with one callback, not one per display change (1111_13879)
1111_13879 on Discord: on Windows the app died after a few hours with
"fatal error: too many callback functions", taking the gateway with it
(clients got connection refused). Five dumps all ended in
w32.GetAllScreens -> syscall.NewCallback -> runtime.compileCallback,
from Wails' WM_DISPLAYCHANGE handling.

Wails (v3.0.0-beta.24 here; v3.1.0 and v3.2.0-nightly.20261101 are the
same) reads the screens again on every WM_DISPLAYCHANGE, and on
WM_SETTINGCHANGE for SPI_SETWORKAREA, through w32.GetAllScreens, which
passes a fresh closure to syscall.NewCallback each call. Go never frees
a callback and throws past about 2000, unrecoverably.

magpie now sets WndProcInterceptor (screensWndProc): on Wails' hidden
main-thread window ("__wails_hidden_mainthread") it takes those two
messages before Wails does, enumerates the monitors with one callback
made once at package level (allScreens), builds the screens exactly as
Wails' processAndCacheScreens does (wailsScreen) and hands them to
Screen.LayoutScreens, then answers as Wails would (DefWindowProc for
WM_DISPLAYCHANGE, 0 for WM_SETTINGCHANGE). Other windows and messages go
on to Wails. Wails' one read at start-up is the only per-call callback
left. No go.mod change: no newer Wails fixes it, and magpie has no fork.

Other per-call NewCallbacks in Wails' Windows code: w32.EnumChildWindows
and w32.SetWindowsHookEx, neither called by Wails or magpie. WndProc
classes are registered once; the WebView2 COM vtables are package-level.
magpie's own (internal/agent apprestart) is already made once.

Run at this tree (base e156fdd03; origin/main since adds only
2251ca1ce, providers, no shared file):
- Windows (win-run, xiaomi-gpu): the three new tests pass;
  TestScreensReadFarPastTheCallbackLimit reads 5000 times and matches
  Wails' own read field for field. go test ./internal/gui: 5 reds,
  TestGatewayHeldByAContainer, TestLodyImportHandoff,
  TestImportStatusUnreadableIsUnknown, TestAionUiImportRow,
  TestUpdateThatWontStart, the same 5 at e156fdd03 without this change
  (#4115, #2107). internal/proc, agentenv, pluginrt ok; sqlitedb's
  TestSQLiteSurvivesMalformedEnv red, the same at the base (#4115).
- mote: gofmt -l clean; go vet ./internal/gui ./internal/cli .;
  GOOS=windows go vet ./...; GOOS=linux go vet -tags nogui ./...;
  go build, GOOS=windows go build, GOOS=linux go build -tags nogui;
  go test ./internal/gui: ok but TestMainWindowCorners (#3949, macOS 27).
- Linux (wsl-run): go test -tags nogui ./... was still running at push (the nogui build leaves these files out), so it had no result to record here.

Broken by hand:
- allScreens with a NewCallback per call (as Wails does):
  TestScreensReadFarPastTheCallbackLimit dies with the reporter's
  "fatal error: too many callback functions" from
  syscall.compileCallback.
- A throwaway test calling Wails' w32.GetAllScreens 5000 times dies the
  same way at w32/screen.go:145.
- WndProcInterceptor left out of windowsOptions:
  TestAppReadsDisplayChangesItself fails.
- wailsMainThread misspelt: TestWailsScreensAreReadOnTheMainThreadWindow
  fails.

Not tried: the real app through real display changes on a desktop
session (win-run has no interactive desktop; the test creates a window
titled as Wails' own and sends it the messages); a multi-monitor or
rotated-display layout (the box has one screen). No upstream Wails issue
filed yet; text drafted.
2026-11-02 20:00:54 +08:00

127 lines
4.4 KiB
Go

//go:build windows && !nogui
package gui
import (
"errors"
"log"
"strconv"
"sync"
"syscall"
"unsafe"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/w32"
)
// Wails reads the screens again on every WM_DISPLAYCHANGE, and on a
// WM_SETTINGCHANGE for the work area, through w32.GetAllScreens. That
// makes a new Win32 callback each time (syscall.NewCallback over a
// closure), and Go never frees one: past about 2000 the runtime throws
// "too many callback functions", which nothing can recover, and the app
// and the gateway it serves die (1111_13879 on Discord, after a few hours
// of display changes). Wails 3.1.0 and the 3.2 nightlies still do it.
//
// screensWndProc takes those two messages on Wails' main-thread window
// before Wails does, and reads the screens with one callback made once
// for the process. Wails' own read at start-up is its only one left.
// screensWndProc is the app's WndProcInterceptor (windowsOptions).
var screensWndProc = rereadScreens
// layoutScreens hands the screens read to Wails, as its own read does.
var layoutScreens = func(s []*application.Screen) error {
app := application.Get()
if app == nil {
return errors.New("no app")
}
return app.Screen.LayoutScreens(s)
}
// screensChanged says whether Wails would read the screens again for msg.
func screensChanged(msg uint32, wParam uintptr) bool {
return msg == w32.WM_DISPLAYCHANGE || msg == w32.WM_SETTINGCHANGE && wParam == w32.SPI_SETWORKAREA
}
func rereadScreens(hwnd uintptr, msg uint32, wParam, lParam uintptr) (uintptr, bool) {
if !screensChanged(msg, wParam) || w32.GetWindowText(hwnd) != wailsMainThread {
return 0, false
}
screens, err := allScreens()
if err == nil {
err = layoutScreens(screens)
}
if err != nil {
// Wails hands its own failure to the app's error handler, which
// logs it; the screens it had stay
log.Printf("screens: %v", err)
}
// What Wails answers after its read: WM_DISPLAYCHANGE goes on to
// DefWindowProc, and every WM_SETTINGCHANGE is answered 0 (its other
// use, ImmersiveColorSet, never comes with SPI_SETWORKAREA).
if msg == w32.WM_DISPLAYCHANGE {
return w32.DefWindowProc(hwnd, msg, wParam, lParam), true
}
return 0, true
}
// The one EnumDisplayMonitors callback, and what it reads into, under
// screensMu. The callback runs on the caller's thread, inside the call.
var (
screensMu sync.Mutex
screensRead []*application.Screen
screensErr error
screensEnumF = syscall.NewCallback(func(hMonitor, _ uintptr, _ *w32.RECT, _ uintptr) uintptr {
info := w32.MONITORINFOEX{MONITORINFO: w32.MONITORINFO{CbSize: uint32(unsafe.Sizeof(w32.MONITORINFOEX{}))}}
if !w32.GetMonitorInfoEx(hMonitor, &info) {
screensErr = errors.New("GetMonitorInfo failed")
return 0
}
var dpiX, dpiY uint
if w32.GetDPIForMonitor(hMonitor, w32.MDT_EFFECTIVE_DPI, &dpiX, &dpiY) != w32.S_OK {
screensErr = errors.New("GetDpiForMonitor failed")
return 0
}
screensRead = append(screensRead, wailsScreen(hMonitor, &info, float32(dpiX)/96))
return 1
})
)
// allScreens is w32.GetAllScreens with the callback made once.
func allScreens() ([]*application.Screen, error) {
screensMu.Lock()
defer screensMu.Unlock()
screensRead, screensErr = nil, nil
ok := w32.EnumDisplayMonitors(0, nil, screensEnumF, nil)
screens, err := screensRead, screensErr
screensRead, screensErr = nil, nil
if !ok {
if err == nil {
err = errors.New("EnumDisplayMonitors failed")
}
return nil, err
}
return screens, nil
}
// wailsScreen is one monitor as Wails' processAndCacheScreens makes it.
func wailsScreen(hMonitor uintptr, info *w32.MONITORINFOEX, scale float32) *application.Screen {
m, w := info.RcMonitor, info.RcWork
x, y := int(m.Left), int(m.Top)
width, height := int(m.Right)-x, int(m.Bottom)-y
work := application.Rect{X: int(w.Left), Y: int(w.Top), Width: int(w.Right - w.Left), Height: int(w.Bottom - w.Top)}
return &application.Screen{
ID: strconv.Itoa(int(hMonitor)),
Name: syscall.UTF16ToString(info.SzDevice[:]),
X: x,
Y: y,
Size: application.Size{Width: width, Height: height},
Bounds: application.Rect{X: x, Y: y, Width: width, Height: height},
PhysicalBounds: application.Rect{X: x, Y: y, Width: width, Height: height},
WorkArea: work,
PhysicalWorkArea: work,
IsPrimary: info.DwFlags == w32.MONITORINFOF_PRIMARY,
ScaleFactor: scale,
}
}