From ba47e132423866099f110321ee73a1f1d56ddf98 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:04:59 -0300 Subject: sync: a terminal's answers to its program (a cursor report, its attributes) and a new shell's greeting go through emitWrite, behind whatever is still queued for its pty, where they were emitted past the parked bytes (a reply jumped a paste) and dropped with the ring full; a test that a reply waits behind a parked paste, which fails on the old loop at byte 262080 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/pardes.zig | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'src/pardes.zig') diff --git a/src/pardes.zig b/src/pardes.zig index 0dc0dc5f..f637e78a 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -7312,13 +7312,11 @@ pub const Pardes = struct { pane.clearPointerSelection(sel_slot); pane.sel[sel_slot].state = .none; } + // What the terminal answers its program (a cursor report, its + // attributes) goes as typed bytes go: behind whatever is still + // queued for its pty, never ahead of it nor dropped. if (comptime terminal_panes) if (pane.terminal) |state| { - var off: u16 = 0; - while (off < state.reply_len) { - const n = @min(state.reply_len - off, 64); - p.emit(.{ .write = .{ .pane = @intCast(id), .bytes = .from(state.reply[off .. off + n]) } }); - off += n; - } + p.emitWrite(id, state.reply[0..state.reply_len]); state.reply_len = 0; }; const r = p.rects[id]; @@ -7414,7 +7412,7 @@ pub const Pardes = struct { .never => pane.greet = false, .now => { p.emit(.{ .resize_pty = .{ .pane = @intCast(id), .cols = pane.cols, .rows = pane.rows } }); - p.emit(.{ .write = .{ .pane = @intCast(id), .bytes = .from("ls\r") } }); + p.emitWrite(id, "ls\r"); pane.greet = false; }, }; @@ -8444,6 +8442,34 @@ test "a bracketed paste larger than the ring still closes its bracket" { try std.testing.expectEqualSlices(u8, text, got["\x1b[200~".len .. got.len - "\x1b[201~".len]); } +test "a terminal's answer to its program waits behind the paste still queued for its pty" { + if (platform == .web) return; + if (comptime !panes.terminal.enabled) return; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); + const buf = try gpa.alloc(u8, 1 << 20); + defer gpa.free(buf); + _ = drainWrites(p, buf); + panes.terminal.enterTty(p, 0); + + const text = try gpa.alloc(u8, 300 * 1024); + defer gpa.free(text); + @memset(text, 'p'); + p.update(.{ .paste = text }); + // The ring is full and the rest of the paste waits for room in it. A + // host takes one write off, and the program asks where its cursor is. + const taken = p.nextEffect().?; + const off = if (taken == .write) taken.write.bytes.len else 0; + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[6n" } }); + const got = drainWrites(p, buf); + // The whole paste first, then the answer, `CSI row ; col R`, whole. + try std.testing.expectEqualSlices(u8, text[off..], got[0 .. text.len - off]); + const answer = got[text.len - off ..]; + try std.testing.expect(std.mem.startsWith(u8, answer, "\x1b[") and std.mem.endsWith(u8, answer, "R")); +} + test "pasted bytes still queued at shutdown are freed, not leaked" { if (platform == .web) return; const gpa = std.testing.allocator; -- cgit v1.3