From 91076bdb892fe898a8b07325867a1d29c1d65862 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 19 Nov 2026 20:47:38 -0300 Subject: dev, runtime: a session's ptys once (dev/Ptys.zig, a working name), the session loop's with the one queue they post to (the Inbox, now the loop's): spawn, take an adopted shell, write, resize, signal, close, the three questions Kill asks and Adopt (Ptys.Calls fills both frontends' Host tables), the readers on the Inbox in both frontends (the terminal's own reader, its two-reads back-pressure and its pty_read/pty_eof messages deleted; it still takes one read a step, the rest kept for the next wait, so a frame shows a command's last line before its exit scrolls it away, as process-test's headersweep watches), end of file and a command's exit, the cwd poll, an Upgrade's pause/record/readOn (a window whose Upgrade failed no longer trips acceptReader's assert), an upgraded session's shells and a Restore's stop (the loop's: the frontend call resumePtys is gone, eleven calls to ten); where the copies differed the window's behaviour stays (the daemon told at a shell's end of file, a reader that cannot start said in its pane, the session's end hangs each shell up and waits out its grace) but the tty's for where a new shell is asked (only one given no directory) and the window's reconcile of ptys against panes is deleted (the core asks every retired terminal pane's pty closed); tui core-test 848/991 (143 skipped), shell-test 1031/1034 (4 Ptys tests, 3 of them moved from gui.zig), term 15/15, pane 183/184, kitty 45/45, behaviour-test and --replay 275/275, the frame corpus 77/77 Debug; on this change's first form (all of a wait's output in one step) the soak 0 diverged, 0 unequal, quiet checks 618/0 835/0 771/0, unlisted 0, corpus scene 22/22, no leak, and gui unit-test 1626/1630 with only the perf check's BaselineHarnessMismatch; the rest of the gate was stopped with the disk under 10 GB (report pardes-services.md) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- dev/gui/gui.zig | 435 +++++--------------------------------------------------- 1 file changed, 37 insertions(+), 398 deletions(-) (limited to 'dev/gui/gui.zig') diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index c147c2e2..49c412ba 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -370,17 +370,6 @@ fn emitTouchClick(g: *const Gui, in: *Input, point: TouchNormPoint, button: pard in.post(.{ .mouse = .{ .button = button, .kind = .release, .col = cell.col, .row = cell.row, .tag_hit = cell.tag_hit, .body_hit = cell.body_hit } }); } -const Pty = struct { - fd: c_int, - pid: libc.pid_t, - serial: u32, - /// Its reader, blocked in an Io read: `closePty` cancels it. - reader: ?std.Io.Future(void) = null, - /// A command pane's child, watched to its exit (host_io.watchExit); - /// the pty stays open until both its end of file and its exit. - cmd: host_io.CommandWatch = .{}, -}; - const Queue = host_io.Inbox; /// Wakes a blocking SDL_WaitEventTimeout from another thread: a push to @@ -1027,9 +1016,6 @@ const Own = struct { g: *Gui, test_mode: bool, detached: bool, - ptys: [pardes.MAX_PANES]?Pty = @splat(null), - gens: [pardes.MAX_PANES]u32 = @splat(0), - queue: Queue = undefined, pipe_tasks: PipeTasks = .{}, inotify_fd: c_int = -1, watch_reader: ?std.Thread = null, @@ -1058,12 +1044,7 @@ const Own = struct { core.view().frontend.native_image_patches = true; observeGuiFont(g, core); syncTaglineFont(g, core); - o.queue = .{ - .io = l.io, - .gpa = l.gpa, - .lsp_allocator = l.allocs.lsp, - .wake_fn = sdlWake, - }; + l.inbox.wake_fn = sdlWake; o.inotify_fd = file_watch.init(true); o.shell = .{ .core = core, @@ -1071,10 +1052,8 @@ const Own = struct { .io = l.io, .gpa = l.gpa, .lsp_allocator = l.allocs.lsp, - .prompt_rcs = &l.prompt_rcs, - .ptys = &o.ptys, - .gens = &o.gens, - .queue = &o.queue, + .ptys = &l.ptys, + .queue = &l.inbox, .pipe_tasks = &o.pipe_tasks, .inotify_fd = o.inotify_fd, .watches = &o.watches, @@ -1102,7 +1081,6 @@ const Own = struct { host_io.setExitWake(null, null); pardes.lsp.setStatusSink(null, null); pardes.Kind.drawer = null; - o.shell.shutdownPtys(); o.shell.job_tasks.cancelAll(io); if (o.inotify_fd >= 0) { if (o.watch_reader) |thread| { @@ -1117,26 +1095,18 @@ const Own = struct { o.inotify_fd = -1; } o.pipe_tasks.cancelAll(io); - if (o.queue.lsp_task) |*task| task.future.cancel(io); + if (l.inbox.lsp_task) |*task| task.future.cancel(io); // Its worker may be waiting on the queue for room. l.core.stream.cancelTask(); - o.queue.deinit(); o.g.hit_core = null; } - pub fn resumePtys(o: *Own, rec: *const upgrade.Record) void { - o.shell.resumePtys(rec); - } - pub fn start(o: *Own, l: *Session) !void { - for (&o.ptys, 0..) |*slot, id| if (slot.*) |*pt| { - pt.reader = try o.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, .at(id), o.gens[id]); - }; if (o.inotify_fd >= 0) { o.watch_stop = try stopPipe(); - o.watch_reader = try std.Thread.spawn(.{}, watchThread, .{ o.inotify_fd, o.watch_stop[0], &o.queue }); + o.watch_reader = try std.Thread.spawn(.{}, watchThread, .{ o.inotify_fd, o.watch_stop[0], &l.inbox }); } - if (l.fs) |f| try f.watch(&o.queue, wakeFs); + if (l.fs) |f| try f.watch(&l.inbox, wakeFs); _ = c.SDL_StartTextInput(o.g.window); // An upgraded session's window, its view's again. @@ -1144,8 +1114,8 @@ const Own = struct { if (o.test_mode) setStdinRaw() catch {}; o.shell.threads_ok = true; - pardes.lsp.setStatusSink(&o.queue, Queue.lspStatus); - host_io.setExitWake(&o.queue, Queue.wakeForExit); + pardes.lsp.setStatusSink(&l.inbox, Queue.lspStatus); + host_io.setExitWake(&l.inbox, Queue.wakeForExit); if (o.detached) { o.shell.closeTheWindow(); // Nothing else may wake the first wait. @@ -1169,10 +1139,9 @@ const Own = struct { pub fn restoring(o: *Own, nc: *pardes.Pardes) void { o.pipe_tasks.cancelAll(o.shell.io); - o.queue.discardCompletions(); + o.shell.queue.discardCompletions(); // One view, the window's (host_io.restoreCore). o.shell.gview = 0; - o.shell.stopPtys(); o.g.cells.clearImages(); o.g.prepared_images.clearRetainingCapacity(); nc.view().frontend.native_images = true; @@ -1498,13 +1467,11 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ window.loop = &loop; // A client spawns, watches and serves nothing: the Shell's tables are // empty ones, there for what draws. - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(0); var queue: Queue = .{ .io = io, .gpa = gpa, .lsp_allocator = start.allocs.lsp, .wake_fn = sdlWake }; defer queue.deinit(); + var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = undefined }; var pipe_tasks: PipeTasks = .{}; var watches: file_watch.Table = @splat(null); - const prompt_rcs: host_io.Shell.PromptFiles = .{}; var no_peers: replica.Peers = .{}; var shell: Shell = .{ .core = start.r.core(), @@ -1512,9 +1479,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ .io = io, .gpa = gpa, .lsp_allocator = start.allocs.lsp, - .prompt_rcs = &prompt_rcs, .ptys = &ptys, - .gens = &gens, .queue = &queue, .pipe_tasks = &pipe_tasks, .inotify_fd = -1, @@ -2434,16 +2399,13 @@ const Shell = struct { /// it). gui: ?*Gui = null, detached: ?*Gui = null, - /// Shells a dead session left (Adopt), each waiting for the spawn of - /// the terminal pane made for it. - adoptions: host_io.Adoptions = .{}, io: std.Io, gpa: std.mem.Allocator, fs: ?*ninep_io.Listener = null, lsp_allocator: std.mem.Allocator, - prompt_rcs: *const host_io.Shell.PromptFiles, - ptys: *[pardes.MAX_PANES]?Pty, - gens: *[pardes.MAX_PANES]u32, + /// The session's ptys (runtime/session.zig's), reading into `queue`; + /// a client's, an empty table. + ptys: *host_io.Ptys, queue: *Queue, /// What drainQueue takes each wakeup, kept rather than rebuilt. batch: Queue.Batch = .{}, @@ -2485,44 +2447,6 @@ const Shell = struct { took_input: bool = false, held: ?c.SDL_Event = null, - fn closePaneShell(ctx: ?*anyopaque, id: pardes.Pane.Id) void { - const pane = id.index(); - shellOf(ctx).closePty(pane); - } - - fn closePty(s: *Shell, pane: u8) void { - const pt = if (s.ptys[pane]) |*pt| pt else return; - // Out of the queue first, which lets go a reader waiting for room - // in it; then its read is canceled (or, at its end, it is joined). - s.queue.cancelReader(.at(pane), s.gens[pane]); - if (pt.reader) |*reader| { - reader.cancel(s.queue.io); // the Io it was started on (startReader) - pt.reader = null; - } - _ = libc.close(pt.fd); - daemon.closed(pt.pid); - host_io.retireShell(pt.pid); - s.ptys[pane] = null; - } - - fn stopPtys(s: *Shell) void { - for (0..s.ptys.len) |pane| s.closePty(@intCast(pane)); - for (s.gens) |*gen| gen.* +%= 1; - } - - fn shutdownPtys(s: *Shell) void { - s.stopPtys(); - host_io.waitRetired(); - } - - fn reconcilePtys(s: *Shell) void { - for (s.ptys, 0..) |slot, pane| if (slot) |pt| { - const current = s.core.panes[pane]; - if (current == null or current.?.serial != pt.serial or !current.?.isTerminal()) - s.closePty(@intCast(pane)); - }; - } - fn host(s: *Shell) pardes.Host { return .{ .ctx = s, .vtable = &vtable }; } @@ -2604,12 +2528,6 @@ const Shell = struct { return shellOf(ctx).gui != null; } - fn adopt(ctx: ?*anyopaque, id: pardes.Pane.Id) void { - const pane = id.index(); - const s = shellOf(ctx); - s.adoptions.ask(s.core, pane); - } - fn upgradeAsked(ctx: ?*anyopaque, id: pardes.Pane.Id) void { const pane = id.index(); const s = shellOf(ctx); @@ -2644,39 +2562,21 @@ const Shell = struct { // the readers stopped (a reader waiting for room has it then), and // what they pushed meanwhile drained. s.drainQueue(); - for (s.ptys) |*slot| if (slot.*) |*pt| if (pt.reader) |*r| { - r.cancel(s.queue.io); - pt.reader = null; - }; + s.ptys.pause(); s.drainQueue(); core.settle(); // Its replica clients told, their views gone before the dump: each // dials the session again once it is back. s.peers.refuseAll(core, "the session is upgrading to another build of pardes"); var rec: upgrade.Record = .{ .link = daemon.link, .window = s.gui != null, .exe = path }; - for (s.ptys, 0..) |slot, id| if (slot) |pt| if (pt.fd >= 0) rec.addPty(id, pt.pid, pt.fd); + s.ptys.record(&rec); var at_buf: [1024:0]u8 = undefined; const failed = upgrade.dumpInto(core, &rec, &at_buf, &why) orelse upgrade.execAs(s.gpa, path, &rec, &at_buf, s.fs, &why); // As it was: its ptys read on. - for (s.ptys, 0..) |*slot, id| if (slot.*) |*pt| if (pt.fd >= 0 and pt.reader == null) { - pt.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, .at(id), s.gens[id]) catch null; - }; + s.ptys.readOn(core); core.reportFailure(pane, failed); } - /// An upgraded session's shells, running on: each its pane's pty again, - /// or, when its dump would not load (`fresh`), adopted as new panes. - fn resumePtys(s: *Shell, rec: *const upgrade.Record) void { - for (rec.ptys[0..rec.nptys]) |k| { - const pane = if (rec.fresh) null else s.core.panes[k.pane]; - if (pane == null or !pane.?.isTerminal()) { - s.adoptions.adoptShell(s.core, k.pid, k.fd); - continue; - } - s.ptys[k.pane] = .{ .fd = k.fd, .pid = k.pid, .serial = pane.?.serial, .cmd = .{ .watched = pane.?.command != null and host_io.watchExit(k.pid), .fd = k.fd } }; - } - } - /// An upgraded session's window, its view's again; every other view /// but a replica client's, kept for its rejoin (views.away), closes. fn resumeWindow(s: *Shell) void { @@ -2695,16 +2595,16 @@ const Shell = struct { .poll_frame = pollFrame, .watched = watched, .detach = detachWindow, - .adopt = adopt, + .adopt = PtyCalls.adopt, .upgrade = upgradeAsked, - .spawn = spawnPane, - .pty_write = ptyWrite, - .pty_resize = ptyResize, - .pty_signal = ptySignal, - .close_pty = closePaneShell, - .tty_taken = ttyTaken, - .fg_name = fgName, - .kill_job = killJob, + .spawn = PtyCalls.spawn, + .pty_write = PtyCalls.write, + .pty_resize = PtyCalls.resize, + .pty_signal = PtyCalls.signal, + .close_pty = PtyCalls.close, + .tty_taken = PtyCalls.ttyTaken, + .fg_name = PtyCalls.fgName, + .kill_job = PtyCalls.killJob, .write_file = writeFile, .write_dump = writeDump, .watch_file = watchFile, @@ -2721,13 +2621,11 @@ const Shell = struct { .job = kindJob, }; + const PtyCalls = host_io.Ptys.Calls(Shell); + /// The command panes' exits, told once their output is in (host_io). fn takeExits(s: *Shell) void { - host_io.takeExits(s.core, s.ptys, s, closeWatched); - } - - fn closeWatched(s: *Shell, id: usize) void { - s.closePty(@intCast(id)); + s.ptys.takeExits(s.core); } /// A client's queue holds its draws alone: each landed for its pane's @@ -2745,7 +2643,6 @@ const Shell = struct { } fn drainQueue(s: *Shell) void { - s.reconcilePtys(); // After this batch's output, which an exit is told behind. defer s.takeExits(); s.queue.takeInto(&s.batch); @@ -2753,39 +2650,8 @@ const Shell = struct { if (msgs.len > 0) s.mail_only = false; var check_files = false; for (msgs.slice()) |m| switch (m) { - .output => |o| { - if (s.gens[o.pane.index()] == o.gen) - s.core.update(.{ .output = .{ .pane = o.pane, .bytes = o.bytes } }); - s.gpa.free(o.bytes); - }, - .eof => |e| { - if (s.gens[e.pane.index()] == e.gen) eof: { - if (e.failure) |err| { - s.closePty(e.pane.index()); - s.core.readerFailed(e.pane, err); - break :eof; - } - const pt = if (s.ptys[e.pane.index()]) |*pt| pt else { - s.core.update(.{ .eof = .{ .pane = e.pane } }); - break :eof; - }; - // A command's pty stays open until its child has exited: - // closing it would hang up one that runs on without it. - // Its exit, if it came first, is told now its output is in. - if (pt.cmd.watched) { - host_io.commandEof(s.core, s.ptys, e.pane.index(), s, closeWatched); - break :eof; - } - // Unwatched, a command's exit is read here, as its end, - // and a shell's, for a run waiting on it and the log. - const unwatched = if (s.core.panes[e.pane.index()]) |pane| pane.command != null else false; - const status = host_io.exitStatus(pt.pid, 100); - if (status != null) pt.pid = 0; // reaped: no shell to retire - s.closePty(e.pane.index()); - if (unwatched or status != null) s.core.update(.{ .exited = .{ .pane = e.pane, .status = status } }); - s.core.update(.{ .eof = .{ .pane = e.pane } }); - } - }, + .output => |o| s.ptys.output(s.core, o), + .eof => |e| s.ptys.eof(s.core, e), .lsp => |l| { s.core.update(.{ .lsp_resp = .{ .id = l.id, .rows = l.rows } }); if (l.rows) |rows| s.lsp_allocator.free(rows); @@ -2821,7 +2687,7 @@ const Shell = struct { .files_changed => check_files = true, .fs_ready => {}, .window => if (s.gui == null) s.openTheWindow(), - .adopted => |a| s.adoptions.adoptShell(s.core, a.pid, a.fd), + .adopted => |a| s.ptys.adoptions.adoptShell(s.core, a.pid, a.fd), }; if (check_files and file_watch.reloadChanged(s.core, s.io, s.gpa, s.watches)) s.queue.push(.files_changed); @@ -3026,11 +2892,10 @@ fn waitDetached(core: *pardes.Pardes, timeout_ms: u32) void { fn pollFrame(ctx: ?*anyopaque) void { const s = shellOf(ctx); - s.reconcilePtys(); const core = s.core; if (s.fs) |f| if (f.tick().pending) s.queue.push(.fs_ready); // The authority owns its terminals even with its window closed. - pollCwds(core, s.ptys); + s.ptys.pollCwds(core); const g = s.gui orelse { if (core.settings.window_opacity_pending) { var unchanged_opacity: u8 = 100; @@ -3330,56 +3195,6 @@ fn guiNow(ctx: ?*anyopaque) u64 { return s.test_clock orelse s.paced_ns orelse c.SDL_GetTicksNS(); } -fn spawnPane(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { - const pane = id.index(); - const s = shellOf(ctx); - if (s.adoptions.take(id)) |a| return takeShell(s, pane, a); - // The new shell starts before a running one goes: one that cannot - // start leaves the running one be (pty/ctl's exec). - const running = s.ptys[pane] != null; - const child = host_io.forkShell(s.core, pane, s.prompt_rcs, s.core.shellBin(), cwd, s.core.view().screen_h, s.core.view().screen_w) catch |err| { - if (running) return s.core.restartFailed(id, err); - return s.core.shellFailed(id, err); - }; - s.closePty(pane); - if (s.ptys[pane] != null) { - _ = libc.close(child.file.handle); - host_io.retireShell(child.pid); - return s.core.reportError(id, "shell", error.WorkersBusy); - } - s.gens[pane] +%= 1; - const command = s.core.panes[pane].?.command != null; - const pt: Pty = .{ .fd = child.file.handle, .pid = child.pid, .serial = s.core.panes[pane].?.serial, .cmd = .{ .watched = command and host_io.watchExit(child.pid), .fd = child.file.handle } }; - s.ptys[pane] = pt; - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.shellCwd(pt.pid, &lbuf)) |wd| s.core.setCwd(id, wd); - if (!s.threads_ok) return; - s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, id, s.gens[pane]) catch |err| { - s.closePty(pane); - return s.core.reportError(id, "terminal reader", err); - }; -} - -/// An adopted shell, running already, as pane `pane`'s: told the size twice -/// (one it never had, then the pane's), which makes it redraw. -fn takeShell(s: *Shell, pane: u8, a: host_io.Adoption) void { - s.closePty(pane); - s.gens[pane] +%= 1; - s.ptys[pane] = .{ .fd = a.fd, .pid = a.pid, .serial = s.core.panes[pane].?.serial, .cmd = .{} }; - var exe_buf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - s.core.acknowledgeShell(.at(pane), host_io.processExe(a.pid, &exe_buf) orelse "sh", host_io.marksPrompts(a.pid)); - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.shellCwd(a.pid, &lbuf)) |wd| s.core.setCwd(.at(pane), wd); - const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; - _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); - if (s.core.panes[pane]) |pn| ptyResize(s, .at(pane), @max(pn.cols, 1), @max(pn.rows, 1)); - if (!s.threads_ok) return; - s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = a.fd, .flags = .{ .nonblocking = false } }, .at(pane), s.gens[pane]) catch |err| { - s.closePty(pane); - return s.core.reportError(.at(pane), "terminal reader", err); - }; -} - /// The daemon's thread: what it says, onto the queue (drainQueue). fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { const s: *Shell = @ptrCast(@alignCast(ctx)); @@ -3401,49 +3216,6 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { } } -fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { - const pane = id.index(); - const s = shellOf(ctx); - if (s.ptys[pane]) |pt| _ = host_io.writeFd(pt.fd, bytes); -} - -fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { - const pane = id.index(); - const s = shellOf(ctx); - if (s.ptys[pane]) |pt| { - const ws = host_io.ptyWinsize(s.core, cols, rows); - _ = posix.system.ioctl(pt.fd, TIOCSWINSZ, @intFromPtr(&ws)); - } -} - -fn ptySignal(ctx: ?*anyopaque, id: pardes.Pane.Id, sig: pardes.PtySignal) void { - const pane = id.index(); - const s = shellOf(ctx); - // A command whose exit is recorded is reaped: its group may be another's. - if (s.ptys[pane]) |pt| if (!pt.cmd.exited) host_io.signalTty(pt.pid, pt.fd, sig, pt.cmd.watched); -} - -fn ttyTaken(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { - const pane = id.index(); - const s = shellOf(ctx); - const pt = s.ptys[pane] orelse return false; - return host_io.ttyTaken(pt.pid, pt.fd); -} - -fn fgName(ctx: ?*anyopaque, id: pardes.Pane.Id, buf: []u8) ?[]const u8 { - const pane = id.index(); - const s = shellOf(ctx); - const pt = s.ptys[pane] orelse return null; - return host_io.foregroundName(pt.pid, pt.fd, buf); -} - -fn killJob(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { - const pane = id.index(); - const s = shellOf(ctx); - const pt = s.ptys[pane] orelse return false; - return host_io.killJob(pt.pid, pt.fd); -} - fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void { const pane = id.index(); const s = shellOf(ctx); @@ -3590,13 +3362,6 @@ fn handOver(ctx: ?*anyopaque, seq: u32, bytes: []const u8) void { }; } -fn pollCwds(core: *pardes.Pardes, ptys: *[pardes.MAX_PANES]?Pty) void { - for (ptys, 0..) |slot, id| if (slot) |pt| { - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.ttyCwd(pt.pid, pt.fd, core.settings.shell.effective.get(), &lbuf)) |cwd| core.setCwd(.at(id), cwd); - }; -} - fn accumulateWheelDelta(pending: f32, raw_y: f32) f32 { if (!std.math.isFinite(raw_y)) return pending; const next = pending - raw_y; @@ -6276,129 +6041,6 @@ test "installed Nerd Symbols fallback covers Yazi directory icons" { try std.testing.expect(atlas.glyphForCodepoint(0xe5ff).glyph != 0); } -test "GUI PTY reader delivers a real shell tail before EOF and joins on close" { - const gpa = std.testing.allocator; - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); - defer core.deinit(); - var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; - defer queue.deinit(); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(1); - var shell: Shell = undefined; - shell.core = core; - shell.gpa = gpa; - shell.ptys = &ptys; - shell.gens = &gens; - shell.queue = &queue; - defer shell.shutdownPtys(); - const rcs: host_io.Shell.PromptFiles = .{}; - const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); - ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; - ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, .at(0), 1); - try std.testing.expect(host_io.writeFd(child.file.handle, "printf '\\120\\101\\122\\104\\105\\123\\055\\124\\101\\111\\114'; exit\n")); - var bytes: std.ArrayList(u8) = .empty; - defer bytes.deinit(gpa); - var eof = false; - const until = std.Io.Clock.awake.now(std.testing.io).toMilliseconds() + 2_000; - while (!eof and std.Io.Clock.awake.now(std.testing.io).toMilliseconds() < until) { - var batch = queue.take(); - defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); - for (batch.slice()) |msg| switch (msg) { - .output => |o| { - try std.testing.expect(!eof); - try bytes.appendSlice(gpa, o.bytes); - }, - .eof => |e| { - try std.testing.expect(e.failure == null); - eof = true; - }, - else => return error.UnexpectedMessage, - }; - if (!eof) try std.testing.io.sleep(.fromMilliseconds(1), .awake); - } - try std.testing.expect(eof); - try std.testing.expect(std.mem.indexOf(u8, bytes.items, "PARDES-TAIL") != null); - shell.closePty(0); - try std.testing.expect(queue.readers[0] == null); - try std.testing.expect(ptys[0] == null or ptys[0].?.reader == null); - try std.testing.expectEqual(@as(c_int, -1), libc.fcntl(child.file.handle, libc.F.GETFD)); -} - -test "GUI PTY Restore joins a real reader waiting for queue space" { - const gpa = std.testing.allocator; - var core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); - defer core.deinit(); - try dump.dumpState(core); - var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; - defer queue.deinit(); - queue.acceptReader(.at(1), 1); - for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = .at(1), .gen = 1, .bytes = try gpa.dupe(u8, "other pane") } }); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(1); - var shell: Shell = undefined; - shell.core = core; - shell.gpa = gpa; - shell.ptys = &ptys; - shell.gens = &gens; - shell.queue = &queue; - defer shell.shutdownPtys(); - const rcs: host_io.Shell.PromptFiles = .{}; - const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); - ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; - ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, .at(0), 1); - try std.testing.expect(host_io.writeFd(child.file.handle, "printf 'after-full'; exit\n")); - try host_io.PtyTests.waitBlocked(&queue); - const old_serial = core.panes[0].?.serial; - const replacement = try dump.restore(core, core.dump_out.?, "test"); - shell.stopPtys(); - core.deinit(); - core = replacement; - shell.core = replacement; - // The dump's pane, its serial too; its old shell stopped all the same. - try std.testing.expectEqual(old_serial, core.panes[0].?.serial); - try std.testing.expectEqual(@as(usize, 0), queue.waiting); - try std.testing.expect(queue.readers[0] == null); - try std.testing.expectEqual(@as(c_int, -1), libc.fcntl(child.file.handle, libc.F.GETFD)); - queue.cancelReader(.at(1), 1); - queue.acceptReader(.at(0), gens[0]); - queue.push(.{ .eof = .{ .pane = .at(0), .gen = 1 } }); - try std.testing.expect(queue.pushOutput(.at(0), gens[0], try gpa.dupe(u8, "fresh"))); - var batch = queue.take(); - defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); - try std.testing.expectEqual(@as(usize, 1), batch.len); - try std.testing.expectEqualStrings("fresh", batch.items[0].output.bytes); -} - -test "GUI PTY deletion joins an idle reader and its shell is reaped by exit" { - const gpa = std.testing.allocator; - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); - defer core.deinit(); - var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; - defer queue.deinit(); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(1); - var shell: Shell = undefined; - shell.core = core; - shell.gpa = gpa; - shell.ptys = &ptys; - shell.gens = &gens; - shell.queue = &queue; - defer shell.shutdownPtys(); - const rcs: host_io.Shell.PromptFiles = .{}; - const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); - ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; - ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, .at(0), 1); - try core.removePane(0, null); - shell.reconcilePtys(); - try std.testing.expect(queue.readers[0] == null); - try std.testing.expectEqual(@as(usize, 0), queue.waiting); - try std.testing.expectEqual(@as(usize, 0), queue.take().len); - try std.testing.expectEqual(@as(c_int, -1), libc.fcntl(child.file.handle, libc.F.GETFD)); - shell.shutdownPtys(); - try std.testing.expectEqual(@as(libc.pid_t, -1), libc.waitpid(child.pid, null, libc.W.NOHANG)); - try std.testing.expectEqual(libc.E.CHILD, libc.errno(-1)); -} - test "GUI PTY file watcher stops and joins without closing its watched descriptor" { if (!file_watch.supported) return error.SkipZigTest; const gpa = std.testing.allocator; @@ -6435,14 +6077,12 @@ test "GUI completion failure survives backlog and leaves the document unchanged" .bytes = try gpa.dupe(u8, "output"), } }); queue.push(.{ .lsp = .{ .id = id, .rows = null } }); - var gens: [pardes.MAX_PANES]u32 = @splat(1); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); + var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = undefined, .gens = @splat(1) }; var shell: Shell = undefined; shell.core = core; shell.queue = &queue; shell.gpa = gpa; shell.lsp_allocator = gpa; - shell.gens = &gens; shell.ptys = &ptys; shell.drainQueue(); try std.testing.expect(core.lsp_ask.wait == null); @@ -8165,16 +7805,15 @@ test "a detached GUI authority follows terminal directory changes with no window _ = core.settings.shell.effective.set("/bin/sh"); var queue: Queue = .{ .io = io, .gpa = gpa, .lsp_allocator = gpa }; defer queue.deinit(); - var ptys: [pardes.MAX_PANES]?Pty = @splat(null); - var gens: [pardes.MAX_PANES]u32 = @splat(1); const rcs: host_io.Shell.PromptFiles = .{}; + var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = &rcs, .gens = @splat(1) }; + defer ptys.deinit(); var pipes: PipeTasks = .{}; var watches: file_watch.Table = @splat(null); var no_peers: replica.Peers = .{}; - var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .prompt_rcs = &rcs, .ptys = &ptys, .gens = &gens, .queue = &queue, .pipe_tasks = &pipes, .inotify_fd = -1, .watches = &watches, .peers = &no_peers }; - defer shell.shutdownPtys(); + var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .ptys = &ptys, .queue = &queue, .pipe_tasks = &pipes, .inotify_fd = -1, .watches = &watches, .peers = &no_peers }; const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); - ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; + ptys.slots[0] = .{ .fd = child.file.handle, .pid = child.pid }; try std.testing.expect(host_io.writeFd(child.file.handle, "cd child\n")); const until = std.Io.Clock.awake.now(io).toMilliseconds() + 2_000; var actual: [4096]u8 = undefined; -- cgit v1.3