From b96d7f270dd20f78cbfe4bc04d480485f947bcf7 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Sun, 29 Nov 2026 13:15:35 -0500 Subject: [PATCH] Fixed race condition for reading changed screen in console. Means we shouldn't be hearing all those random space characters any more. --- internal/app/screen_policy_test.go | 41 ++++++++++++ internal/app/screen_updates.go | 5 ++ internal/console/source_linux.go | 18 +++++- internal/console/source_linux_test.go | 91 +++++++++++++++++++++++++++ internal/screen/screen.go | 9 ++- internal/version/version.go | 2 +- 6 files changed, 162 insertions(+), 4 deletions(-) diff --git a/internal/app/screen_policy_test.go b/internal/app/screen_policy_test.go index b2605965..8c8869d3 100644 --- a/internal/app/screen_policy_test.go +++ b/internal/app/screen_policy_test.go @@ -5,6 +5,7 @@ package app import ( "context" + "fmt" "os" "reflect" "strings" @@ -16,6 +17,46 @@ import ( "git.stormux.org/storm/fenrir/internal/screen" ) +func TestUnstableCapturePreservesTypingAndReaderState(t *testing.T) { + source := &fakeSource{snapshot: screen.Snapshot{Terminal: 2, Active: true, Rows: 1, Cols: 5, X: 2, Cells: []rune("$ ")}} + voice := &fakeVoice{} + a, err := newApplication(context.Background(), source, &fakeKeys{}, voice, RunOptions{Settings: config.Defaults()}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(a.ui.close) + if err := a.readScreen(); err != nil { + t.Fatal(err) + } + voice.messages = nil + a.engine.NoteInput(true, true, false) + source.readErr = fmt.Errorf("%w: console cursor moved", screen.ErrChanged) + if err := a.readScreen(); err != nil { + t.Fatal("unstable capture stopped the reader", err) + } + if !a.lastActive || a.lastTerminal != 2 || a.lastSnapshot.X != 2 || len(voice.messages) != 0 { + t.Fatal("unstable capture changed reader state", a.lastSnapshot, voice.messages) + } + source.readErr = nil + source.snapshot.X = 3 + source.snapshot.Cells = []rune("$ r ") + if err := a.readScreen(); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(voice.messages, []string{"r"}) { + t.Fatal("retry lost typing feedback", voice.messages) + } + voice.messages = nil + a.engine.NoteInput(true, false, false) + source.snapshot.X = 4 + if err := a.readScreen(); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(voice.messages, []string{"space"}) { + t.Fatal("real cursor navigation lost space feedback", voice.messages) + } +} + func TestLoginPromptStartupAndDelayedActivation(t *testing.T) { for _, terminal := range []int{1, 2} { // tty1 already has its boot login prompt. tty2's login program diff --git a/internal/app/screen_updates.go b/internal/app/screen_updates.go index 94a80a0d..71511d0a 100644 --- a/internal/app/screen_updates.go +++ b/internal/app/screen_updates.go @@ -18,12 +18,14 @@ package app import ( + "errors" "fmt" "time" "git.stormux.org/storm/fenrir/internal/diagnostic" "git.stormux.org/storm/fenrir/internal/i18n" "git.stormux.org/storm/fenrir/internal/presentation" + "git.stormux.org/storm/fenrir/internal/screen" "git.stormux.org/storm/fenrir/internal/spellcheck" ) @@ -31,6 +33,9 @@ func (a *application) readScreen() error { started := time.Now() snapshot, err := a.source.Read() diagnostic.Timing("screen capture", started) + if errors.Is(err, screen.ErrChanged) { + return nil + } if err != nil { return err } diff --git a/internal/console/source_linux.go b/internal/console/source_linux.go index 7fa14873..6f2d7f34 100644 --- a/internal/console/source_linux.go +++ b/internal/console/source_linux.go @@ -178,8 +178,15 @@ func (s *Source) Read() (screen.Snapshot, error) { } } snapshot, err := s.read() - if err == nil && s.watcher != nil { - err = s.watcher.arm(int(s.changes.Fd())) + if s.watcher != nil && (err == nil || errors.Is(err, screen.ErrChanged)) { + if armErr := s.watcher.arm(int(s.changes.Fd())); armErr != nil { + return screen.Snapshot{}, armErr + } + if errors.Is(err, screen.ErrChanged) { + // Yield to the event loop after bounded retries. Another capture + // is needed even if output stopped during the final attempt. + s.watcher.notify() + } } return snapshot, err } @@ -228,6 +235,13 @@ func (s *Source) read() (screen.Snapshot, error) { lastErr = fmt.Errorf("console resized during screen read") continue } + // VCSA and VCSU are separate reads. Typing between them can pair + // new text with the old cursor, causing the next update to speak + // the blank after the typed character as cursor navigation. + if check[2] != header[2] || check[3] != header[3] { + lastErr = fmt.Errorf("%w: console cursor moved", screen.ErrChanged) + continue + } snapshot.NumLock = numLock snapshot.CapsLock = capsLock return snapshot, nil diff --git a/internal/console/source_linux_test.go b/internal/console/source_linux_test.go index 9c7c729c..f2a6aa44 100644 --- a/internal/console/source_linux_test.go +++ b/internal/console/source_linux_test.go @@ -25,10 +25,101 @@ import ( "fmt" "os" "path/filepath" + "reflect" "strings" "testing" + + "git.stormux.org/storm/fenrir/internal/presentation" + "git.stormux.org/storm/fenrir/internal/reader" + "git.stormux.org/storm/fenrir/internal/screen" ) +func TestSourceRetriesCursorChangeDuringTyping(t *testing.T) { + for _, trial := range []struct { + name string + char rune + x, y byte + nextX, nextY byte + busy bool + }{ + {"letter r", 'r', 2, 0, 3, 0, false}, + {"letter a", 'a', 2, 0, 3, 0, false}, + {"punctuation", '.', 2, 0, 3, 0, false}, + {"line wrap", 'r', 4, 0, 0, 1, false}, + {"busy console", 'r', 2, 0, 3, 0, true}, + } { + t.Run(trial.name, func(t *testing.T) { + root := t.TempDir() + write := func(path string, data []byte) { + t.Helper() + path = filepath.Join(root, path) + if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, data, 0600); err != nil { + t.Fatal(err) + } + } + write("sys/devices/virtual/tty/tty0/active", []byte("tty2\n")) + attributes := append([]byte{2, 5, trial.x, trial.y}, make([]byte, 20)...) + write("dev/vcsa2", attributes) + var text []byte + for range 10 { + text = binary.NativeEndian.AppendUint32(text, ' ') + } + write("dev/vcsu2", text) + source := &Source{root: root, state: func(string) (bool, bool, bool, error) { + return true, false, false, nil + }} + before, err := source.Read() + if err != nil { + t.Fatal(err) + } + engine := reader.New() + engine.Update(before) + engine.NoteInput(true, true, false) + // Model typing between the VCSA capture and the VCSU read: text + // is new, but the first captured header still has the old cursor. + binary.NativeEndian.PutUint32(text[(int(trial.y)*5+int(trial.x))*4:], uint32(trial.char)) + write("dev/vcsu2", text) + calls := 0 + source.state = func(string) (bool, bool, bool, error) { + calls++ + if calls == 2 { + attributes[2], attributes[3] = trial.nextX, trial.nextY + write("dev/vcsa2", attributes) + } + if trial.busy && (calls == 4 || calls == 6) { + attributes[2], attributes[3] = trial.x, trial.y + if calls == 6 { + attributes[2], attributes[3] = trial.nextX, trial.nextY + } + write("dev/vcsa2", attributes) + } + return true, false, false, nil + } + if trial.busy { + if _, err := source.Read(); !errors.Is(err, screen.ErrChanged) || calls != 6 { + t.Fatalf("busy capture must yield after three attempts: calls=%d error=%v", calls, err) + } + } + var output []presentation.Message + // Include the next notification, which used to announce the + // blank under the cursor after already speaking the typed letter. + for range 2 { + snapshot, err := source.Read() + if err != nil { + t.Fatal(err) + } + output = append(output, engine.Update(snapshot)...) + } + if want := []presentation.Message{presentation.CharacterMessage(trial.char)}; !reflect.DeepEqual(output, want) { + t.Fatalf("typing feedback = %#v, want %#v", output, want) + } + }) + } +} + func TestSourceWaitsForConsoleOwner(t *testing.T) { for _, terminal := range []int{2, 15} { t.Run(fmt.Sprint(terminal), func(t *testing.T) { diff --git a/internal/screen/screen.go b/internal/screen/screen.go index ed9b711d..aabc6be7 100644 --- a/internal/screen/screen.go +++ b/internal/screen/screen.go @@ -18,7 +18,14 @@ // Package screen describes the visible cells of one terminal session. package screen -import "strings" +import ( + "errors" + "strings" +) + +// ErrChanged means capture could not obtain a consistent snapshot. The source +// schedules another event; callers must preserve the previous reader state. +var ErrChanged = errors.New("screen changed during capture") type Snapshot struct { Terminal int diff --git a/internal/version/version.go b/internal/version/version.go index 56836f98..7d437463 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -5,7 +5,7 @@ package version // Keep editable release metadata here, following Cthulhu's version package. -const Version = "2026.11.03" +const Version = "2026.11.29" const Channel = "testing" func Label() string {