From f63831915559016aeab49b049c85c60c623a0ee9 Mon Sep 17 00:00:00 2001 From: Jason Tackaberry Date: Wed, 30 Sep 2026 18:46:32 -0400 Subject: [PATCH] fix(input): wait for an ST split across reads A read that ended between the ESC and the backslash of an OSC or APC terminator made the decoder cancel the sequence, dropping it and reporting the backslash as a key. Long replies cross read boundaries constantly (kitty's OSC 5522 clipboard data arrives in thousands of 5 KiB packets), so the sequence is incomplete, not cancelled. The reader now waits for the rest, and only treats it as cancelled if the escape timeout passes first. --- decoder.go | 14 ++++++++++++-- event.go | 7 +++++++ key_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ terminal_reader.go | 9 ++++++++- 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/decoder.go b/decoder.go index 65d3f0bd..efcccffc 100644 --- a/decoder.go +++ b/decoder.go @@ -815,7 +815,13 @@ func (p *EventDecoder) parseOsc(b []byte) (int, Event) { case ansi.CAN, ansi.SUB: return i, ignoredEvent(b[:i]) case ansi.ESC: - if i >= len(b) || b[i] != '\\' { + if i >= len(b) { + // The input ends between the ESC and the backslash of an ST. + // A read boundary can fall there in any long sequence, so + // it's incomplete until more input says otherwise. + return i, incompleteEvent(b[:i]) + } + if b[i] != '\\' { if cmd == -1 || (start == 0 && end == 2) { return 2, defaultKey() } @@ -909,7 +915,11 @@ func (p *EventDecoder) parseStTerminated(intro8, intro7 byte, fn func([]byte) Ev case ansi.CAN, ansi.SUB: return i, ignoredEvent(b[:i]) case ansi.ESC: - if i >= len(b) || b[i] != '\\' { + if i >= len(b) { + // Incomplete, as in parseOsc. + return i, incompleteEvent(b[:i]) + } + if b[i] != '\\' { if start == end { return defaultKey(b) } diff --git a/event.go b/event.go index c0b9eb06..039e6a5f 100644 --- a/event.go +++ b/event.go @@ -572,3 +572,10 @@ type ProgramStatusSupportEvent struct{} // ignoredEvent represents a sequence event that is ignored by the terminal // reader. This is used to ignore certain sequences that can be canceled. type ignoredEvent string + +// incompleteEvent represents a string sequence (OSC, APC, and the like) cut +// off between the ESC and the backslash of its ST terminator, which is +// where the input ends. The terminal reader waits for more input. If none +// comes before the escape timeout, the lone ESC cancelled the sequence +// after all, and it's ignored. +type incompleteEvent string diff --git a/key_test.go b/key_test.go index ab0988fb..8ac9e37b 100644 --- a/key_test.go +++ b/key_test.go @@ -2759,3 +2759,47 @@ func TestKeyStringMore(t *testing.T) { }) } } + +// chunkedReader returns its chunks one Read at a time, the way a terminal's +// input arrives in pieces that needn't line up with the sequences in it. +type chunkedReader struct{ chunks [][]byte } + +func (r *chunkedReader) Read(p []byte) (int, error) { + if len(r.chunks) == 0 { + return 0, io.EOF + } + n := copy(p, r.chunks[0]) + if n < len(r.chunks[0]) { + r.chunks[0] = r.chunks[0][n:] + } else { + r.chunks = r.chunks[1:] + } + return n, nil +} + +// A read that ends between the ESC and the backslash of a sequence's ST +// terminator leaves the sequence incomplete, not cancelled. It must come +// out whole once the backslash arrives, rather than be dropped with the +// backslash reported as a key. Large OSC replies, such as kitty's OSC 5522 +// clipboard data sent in thousands of 5 KiB packets, cross read boundaries +// constantly, so this isn't rare in practice. +func TestReadInputSplitTerminator(t *testing.T) { + for _, seq := range []string{ + "\x1b]5522;type=read:status=DATA;QUJD\x1b\\", + "\x1b_hello\x1b\\", + } { + cut := len(seq) - 1 // the first read ends on the terminator's ESC + in := &chunkedReader{chunks: [][]byte{[]byte(seq[:cut]), []byte(seq[cut:] + "a")}} + events := testReadInputs(t, in) + var want []Event + switch seq[1] { + case ']': + want = []Event{UnknownOscEvent(seq), KeyPressEvent{Code: 'a', Text: "a"}} + case '_': + want = []Event{UnknownApcEvent(seq), KeyPressEvent{Code: 'a', Text: "a"}} + } + if !reflect.DeepEqual(events, want) { + t.Errorf("%q split before its final backslash:\n got %#v\nwant %#v", seq, events, want) + } + } +} diff --git a/terminal_reader.go b/terminal_reader.go index b3b79cd0..9bee21c2 100644 --- a/terminal_reader.go +++ b/terminal_reader.go @@ -317,7 +317,7 @@ func (d *eventScanner) scanEvents(buf []byte, expired bool) (total int, events [ d.paste = append(d.paste, seq...) } } - case UnknownEvent: + case UnknownEvent, incompleteEvent: if !expired { // If the event is unknown and we are not expired, we // return need to try to decode the buffer again. @@ -337,6 +337,13 @@ func (d *eventScanner) scanEvents(buf []byte, expired bool) (total int, events [ case ignoredEvent: // ignore this event event = nil + case incompleteEvent: + // Wait for the rest of the sequence, unless the escape timeout + // has passed without it, when it counts as cancelled. + if !expired { + return total, events + } + event = nil case UnknownEvent: isUnknown = true // Try to look up the event in the table.