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

+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) {