diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-05 00:57:44 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:27:20 -0300 |
| commit | 0de5fd5523faf4028b5c5e334367d28b4079aec3 (patch) | |
| tree | 5e059853da6a4a898291a429892cc66dd7ca31d9 | |
| parent | e50c1b3fc928bf860bc6e91b0a2fea469f7e41e7 (diff) | |
| download | pardes-0de5fd5523faf4028b5c5e334367d28b4079aec3.tar.gz pardes-0de5fd5523faf4028b5c5e334367d28b4079aec3.zip | |
host_io: the shells a dead session left are one Adoptions table (Adopt's ask, adoptShell, a spawn's take, and the Adoption a terminal pane takes), where the tty and the window each kept the same three fields and the same adopt, adoptShell and spawn lookup
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/gui/gui.zig | 46 | ||||
| -rw-r--r-- | src/host_io.zig | 56 | ||||
| -rw-r--r-- | src/tui/tui.zig | 51 |
3 files changed, 69 insertions, 84 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index ecd9026f..8415d64e 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -4115,9 +4115,7 @@ const Shell = struct { grid: bool = false, /// Shells a dead session left (Adopt), each waiting for the spawn of /// the terminal pane made for it. - adoptions: [pardes.MAX_PANES]Adoption = undefined, - adoptions_len: usize = 0, - adopted: usize = 0, + adoptions: host_io.Adoptions = .{}, io: std.Io, gpa: std.mem.Allocator, fs: ?*ninep_io.Listener = null, @@ -4429,9 +4427,7 @@ const Shell = struct { fn adopt(ctx: ?*anyopaque, pane: u8) void { const s = shellOf(ctx); - if (!daemon.kept()) return s.core.reportFailure(pane, "Adopt: no pardes daemon is keeping shells for this session"); - s.adopted = 0; - daemon.adopt(); + s.adoptions.ask(s.core, pane); } fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { @@ -4510,7 +4506,7 @@ const Shell = struct { 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.adoptShell(k.pid, k.fd); + 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 } }; @@ -4532,30 +4528,6 @@ const Shell = struct { s.follow(); } - /// One shell of a dead session (`pid` 0: the last of them was told): - /// a terminal pane made for it, as the Term word makes one, whose spawn - /// takes this pty (spawnPane). - fn adoptShell(s: *Shell, pid: libc.pid_t, fd: c_int) void { - const core = s.core; - if (pid == 0) { - var buf: [96]u8 = undefined; - return core.setMessage(core.active, if (s.adopted == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ s.adopted, if (s.adopted == 1) "" else "s" }) catch "Adopt: shells taken in"); - } - if (s.adoptions_len < s.adoptions.len) { - s.adoptions[s.adoptions_len] = .{ .pid = pid, .fd = fd, .pane = null }; - s.adoptions_len += 1; - if (core.adoptShell()) |id| { - s.adoptions[s.adoptions_len - 1].pane = id; - s.adopted += 1; - return; - } - s.adoptions_len -= 1; - } - _ = libc.close(fd); - var buf: [96]u8 = undefined; - core.setMessage(core.active, std.fmt.bufPrint(&buf, "Adopt: no room for shell {d}; it ends with this session", .{pid}) catch "Adopt: no room for a shell"); - } - const vtable: pardes.Host.VTable = .{ .wait_input = waitInput, .now = guiNow, @@ -4698,7 +4670,7 @@ const Shell = struct { s.saw_event = true; }, .adopted => |a| { - s.adoptShell(a.pid, a.fd); + s.adoptions.adoptShell(s.core, a.pid, a.fd); s.saw_event = true; }, }; @@ -5258,11 +5230,7 @@ fn gridPostPresent(ctx: ?*anyopaque) void { fn spawnPane(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { const s = shellOf(ctx); s.reap(); - for (s.adoptions[0..s.adoptions_len], 0..) |a, i| if (a.pane == pane) { - std.mem.copyForwards(Adoption, s.adoptions[i .. s.adoptions_len - 1], s.adoptions[i + 1 .. s.adoptions_len]); - s.adoptions_len -= 1; - return takeShell(s, pane, a); - }; + if (s.adoptions.take(pane)) |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; @@ -5289,11 +5257,9 @@ fn spawnPane(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { }; } -const Adoption = struct { pid: libc.pid_t, fd: c_int, pane: ?u8 }; - /// 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: Adoption) void { +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 = .{} }; diff --git a/src/host_io.zig b/src/host_io.zig index 3a9a73fd..6ee38b6b 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -8,6 +8,7 @@ const ninep_io = @import("9p_io.zig"); const filesystem = @import("fs.zig"); const file_watch = @import("file_watch.zig"); const replica = @import("replica.zig"); +const daemon = @import("daemon.zig"); pub const Host = struct { ctx: ?*anyopaque = null, @@ -120,6 +121,61 @@ pub fn restoreCore(gpa: std.mem.Allocator, core: *pardes.Pardes, path: []const u return nc; } +/// A shell of a dead session the daemon handed this one (Adopt), and the +/// terminal pane made for it once there is one. +pub const Adoption = struct { pid: libc.pid_t, fd: c_int, pane: ?u8 }; + +/// The shells of dead sessions handed this one (Adopt), each waiting for +/// the spawn of the terminal pane made for it (`take`). +pub const Adoptions = struct { + list: [pardes.MAX_PANES]Adoption = undefined, + len: usize = 0, + /// Taken in since the last Adopt. + taken: usize = 0, + + /// Adopt: the daemon is asked for the shells of sessions that died + /// holding them; each comes back to `adoptShell`. + pub fn ask(a: *Adoptions, core: *pardes.Pardes, pane: u8) void { + if (!daemon.kept()) return core.reportFailure(pane, "Adopt: no pardes daemon is keeping shells for this session"); + a.taken = 0; + daemon.adopt(); + } + + /// One shell of a dead session (`pid` 0: the last of them was told): + /// a terminal pane made for it, as the Term word makes one, whose spawn + /// takes this pty instead of forking a shell (`take`). + pub fn adoptShell(a: *Adoptions, core: *pardes.Pardes, pid: libc.pid_t, fd: c_int) void { + if (pid == 0) { + var buf: [96]u8 = undefined; + return core.setMessage(core.active, if (a.taken == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ a.taken, if (a.taken == 1) "" else "s" }) catch "Adopt: shells taken in"); + } + if (a.len < a.list.len) { + a.list[a.len] = .{ .pid = pid, .fd = fd, .pane = null }; + a.len += 1; + if (core.adoptShell()) |id| { + a.list[a.len - 1].pane = id; + a.taken += 1; + return; + } + a.len -= 1; + } + // No room for it: its master goes, and the shell with this session. + _ = libc.close(fd); + var buf: [96]u8 = undefined; + core.setMessage(core.active, std.fmt.bufPrint(&buf, "Adopt: no room for shell {d}; it ends with this session", .{pid}) catch "Adopt: no room for a shell"); + } + + /// The adopted shell pane `pane`'s spawn takes, off the list. + pub fn take(a: *Adoptions, pane: u8) ?Adoption { + for (a.list[0..a.len], 0..) |it, i| if (it.pane == pane) { + std.mem.copyForwards(Adoption, a.list[i .. a.len - 1], a.list[i + 1 .. a.len]); + a.len -= 1; + return it; + }; + return null; + } +}; + pub const Fallback = struct { pub const dump_path = "pardes.dump"; diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 5c16f1be..2a107dc4 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -1654,7 +1654,7 @@ fn localSession( _ = libc.close(pt.file.handle); slot.* = null; }; - for (sh.adoptions[0..sh.adoptions_len]) |a| _ = libc.close(a.fd); + for (sh.adoptions.list[0..sh.adoptions.len]) |a| _ = libc.close(a.fd); if (sh.lsp_task) |*t| { t.future.cancel(io); sh.lsp_task = null; @@ -1799,8 +1799,6 @@ fn clearNativeImages( kitty_handles.clearRetainingCapacity(); } -const Adoption = struct { pid: posix.pid_t, fd: posix.fd_t, pane: ?u8 }; - /// The daemon's thread: what it says, onto the loop's queue. fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { const loop: *Loop = @ptrCast(@alignCast(ctx)); @@ -1849,9 +1847,7 @@ const Shell = struct { inotify_fd: c_int, /// Shells a dead session left (Adopt), each waiting for the spawn of /// the terminal pane made for it. - adoptions: [pardes.MAX_PANES]Adoption = undefined, - adoptions_len: usize = 0, - adopted: usize = 0, + adoptions: host_io.Adoptions = .{}, fs: ?*ninep_io.Listener = null, ptys: [pardes.MAX_PANES]?Pty = @splat(null), gens: [pardes.MAX_PANES]u32 = @splat(0), @@ -2086,13 +2082,9 @@ const Shell = struct { if (stub >= 0) daemon.refuseStub(stub, why); } - /// Adopt: the daemon is asked for the shells of sessions that died - /// holding them; each comes back (`adopted`) as a terminal pane here. fn adopt(ctx: ?*anyopaque, pane: u8) void { const s = of(ctx); - if (!daemon.kept()) return s.core.reportFailure(pane, "Adopt: no pardes daemon is keeping shells for this session"); - s.adopted = 0; - daemon.adopt(); + s.adoptions.ask(s.core, pane); } /// A client's own build is not the session's to say (its core's host @@ -2193,7 +2185,7 @@ const Shell = struct { 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.adoptShell(k.pid, k.fd); + s.adoptions.adoptShell(s.core, k.pid, k.fd); continue; } const file: std.Io.File = .{ .handle = k.fd, .flags = .{ .nonblocking = false } }; @@ -2254,31 +2246,6 @@ const Shell = struct { if (s.terms[core.view] == null) s.terms[core.view] = primary; } - /// One shell of a dead session (`pid` 0: the last of them was told). - fn adoptShell(s: *Shell, pid: posix.pid_t, fd: posix.fd_t) void { - const core = s.core; - if (pid == 0) { - var buf: [96]u8 = undefined; - return core.setMessage(core.active, if (s.adopted == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ s.adopted, if (s.adopted == 1) "" else "s" }) catch "Adopt: shells taken in"); - } - if (s.adoptions_len < s.adoptions.len) { - // A terminal pane, as the Term word makes one; its spawn takes - // this pty instead of forking a shell (`spawn`). - s.adoptions[s.adoptions_len] = .{ .pid = pid, .fd = fd, .pane = null }; - s.adoptions_len += 1; - if (core.adoptShell()) |id| { - s.adoptions[s.adoptions_len - 1].pane = id; - s.adopted += 1; - return; - } - s.adoptions_len -= 1; - } - // No room for it: its master goes, and the shell with this session. - _ = libc.close(fd); - var buf: [96]u8 = undefined; - core.setMessage(core.active, std.fmt.bufPrint(&buf, "Adopt: no room for shell {d}; it ends with this session", .{pid}) catch "Adopt: no room for a shell"); - } - fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const s = of(ctx); // The wait is the 9P connections' turn with the core. A timeout (the @@ -2486,7 +2453,7 @@ const Shell = struct { return true; }, .attach => |a| s.attachTerminal(a.tty, a.stub, null), - .adopted => |a| s.adoptShell(a.pid, a.fd), + .adopted => |a| s.adoptions.adoptShell(s.core, a.pid, a.fd), .replica => |j| { s.peers.handed(j.fd, j.view); s.input_seen = true; @@ -2805,11 +2772,7 @@ const Shell = struct { fn spawn(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { const s = of(ctx); - for (s.adoptions[0..s.adoptions_len], 0..) |a, i| if (a.pane == pane) { - std.mem.copyForwards(Adoption, s.adoptions[i .. s.adoptions_len - 1], s.adoptions[i + 1 .. s.adoptions_len]); - s.adoptions_len -= 1; - return s.takeShell(pane, a); - }; + if (s.adoptions.take(pane)) |a| return s.takeShell(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; @@ -2835,7 +2798,7 @@ const Shell = struct { /// An adopted shell, running already, as pane `pane`'s: told the size /// twice (a size it never had, then the pane's), which makes it redraw. - fn takeShell(s: *Shell, pane: u8, a: Adoption) void { + fn takeShell(s: *Shell, pane: u8, a: host_io.Adoption) void { closePty(s, pane); s.gens[pane] +%= 1; const file: std.Io.File = .{ .handle = a.fd, .flags = .{ .nonblocking = false } }; |
