Fixed race condition for reading changed screen in console. Means we shouldn't be hearing all those random space characters any more.

This commit is contained in:
Storm Dragon committed 2026-11-29 13:15:35 -05:00
1 parent 6a02035373
commit b96d7f270d
6 files changed
+162 -4

No files matched your search

+41
View File
@@ -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
+5
View File
@@ -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
}
+16 -2
View File
@@ -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
+91
View File
@@ -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) {
+8 -1
View File
@@ -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
+1 -1
View File
@@ -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 {