Make state single-owner and stabilize race detector

Frame now carries view state (scale/focus/query) to the main goroutine,
which reads only the frame-receiver-stored snapshot. Renderer owns Gio
widget editors (registered by ID) and the draw scale. Autosave timer
sends a token to the logic goroutine instead of touching state;
Shutdown waits for the owner to exit before FlushAll.

Tests access state only through owner-side Inspect/WithState helpers
(harness + in-package). Fixed double-Harness.Run race in two e2e tests,
made TestWorkerPool_PriorityPreemption deterministic, and raised the
lazy-loading test timeout that was too short under -race.

go test -race ./... is now green.
This commit is contained in:
gmp
2026-08-16 01:32:27 -04:00
parent 7240b62a61
commit 58725a5e6c
20 changed files with 723 additions and 369 deletions
+29 -19
View File
@@ -13,6 +13,7 @@ import (
"gioui.org/unit"
"gioui.org/font/gofont"
"gioui.org/io/key"
"gioui.org/widget"
"pad/internal/editor"
"pad/internal/io/pool/real"
@@ -53,13 +54,22 @@ func run(w *app.Window) error {
log.Printf("using filesystem at / (startup directory: %s)", startAbs)
logic := editor.NewLogic(fs, startAbs, OpenFile)
renderer := ui.New(ui.Theme{FontSize: 14}, shaper, logic.State())
renderer := ui.New(ui.Theme{FontSize: 14}, shaper)
// The search bar's widget.Editor is owned by the MAIN goroutine: Gio
// mutates it during draw, and the logic goroutine must never touch it
// (architecture.md §1). Its text is forwarded to the logic goroutine via
// SearchQueryChan; the logic only stores the result in Browser.Query.
var searchEditor widget.Editor
renderer.RegisterGioEditor("search_bar", &searchEditor)
// frame is the frame-receiver-stored handoff (architecture.md §2.2/§9):
// the ONLY data the main goroutine reads from the logic side.
var mu sync.Mutex
var elems []ui.Element
var curScale, newScale float32
var frame editor.Frame
log.Printf("run: starting frameReceiver")
go frameReceiver(w, &mu, &elems, logic.FrameChan())
go frameReceiver(w, &mu, &frame, logic.FrameChan())
log.Printf("run: starting logic.Run")
go logic.Run()
@@ -76,24 +86,24 @@ func run(w *app.Window) error {
}
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
newScale = gtx.Metric.PxPerDp
curScale = logic.State().Scale()
// Deadlock risk: ensure no channel sends while lock is held
newScale := gtx.Metric.PxPerDp
// Read ONLY the frame-receiver-stored snapshot; the main goroutine
// never touches logic State (architecture.md §1).
mu.Lock()
currentElems := elems
renderer.Draw(gtx, currentElems)
curScale := frame.Scale
if curScale <= 0 {
curScale = 1 // no frame yet
}
renderer.Draw(gtx, frame.Elems, curScale)
glyphLayout := renderer.GlyphLayout()
// Send search query update to the logic goroutine when it changes.
// The logic goroutine handles filtering and triggers a new frame.
newQuery := logic.State().Browser.SearchEditor.Text()
sendQuery := false
if newQuery != logic.State().Browser.Query {
sendQuery = true
}
newQuery := searchEditor.Text()
sendQuery := newQuery != frame.Query
events := renderer.CheckGestures(e.Source, gtx.Metric)
// Gather key events
focusedID := logic.State().FocusedElementID
focusedID := frame.FocusedElementID
if focusedID != "" {
if reg, ok := renderer.Keys[focusedID]; ok {
// Use key.Filter to only receive events destined for the focused element.
@@ -134,7 +144,7 @@ func run(w *app.Window) error {
e.Frame(&ops)
mu.Unlock()
if newScale != curScale {
logic.ConfigChan() <- editor.ScaleEvent{newScale}
logic.ConfigChan() <- editor.ScaleEvent{Scale: newScale}
}
if len(events) > 0 {
logic.InputChan() <- events
@@ -150,13 +160,13 @@ func run(w *app.Window) error {
}
}
func frameReceiver(w *app.Window, mu *sync.Mutex, elems *[]ui.Element, frameChan <-chan []ui.Element) {
func frameReceiver(w *app.Window, mu *sync.Mutex, frame *editor.Frame, frameChan <-chan editor.Frame) {
log.Printf("frameReceiver: loop starting")
for {
frame := <-frameChan
f := <-frameChan
//log.Printf("frameReceiver: received frame")
mu.Lock()
*elems = frame
*frame = f
w.Invalidate()
mu.Unlock()
}