summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-28 10:33:40 -0300
committerGabriel Schneider <[email protected]>2026-10-01 00:12:14 -0300
commitd8382d6ea6692cf07ec6df5a7f2b86ad1f01a9df (patch)
tree0cafa5687f718e897c214ffbf9c02cc642b23006
parent60e40675c82efd17aa6bf31bc66c3f8f3f918360 (diff)
downloadpardes-d8382d6ea6692cf07ec6df5a7f2b86ad1f01a9df.tar.gz
pardes-d8382d6ea6692cf07ec6df5a7f2b86ad1f01a9df.zip
A terminal pane that closes takes its shell and pty with it, in every front end
The detached server closed a pane's pty only when the shell was respawned, hit EOF or the session shut down, so rmdir, Del or Delcol on a terminal left its shell running with nobody to read it; the tty front end and macOS did the same. Retiring a terminal pane now emits a close_pty effect, which each host that runs shells answers by hanging the pty up and ending the shell (the detached server's and the GUI's existing retire-and-reap path, and a close plus SIGHUP in the tty and macOS shells). Co-Authored-By: Claude Opus 5.5 <[email protected]>
-rw-r--r--src/detached/server.zig44
-rw-r--r--src/gui/gui.zig5
-rw-r--r--src/host_io.zig1
-rw-r--r--src/macos.zig10
-rw-r--r--src/ninep/pty.zig12
-rw-r--r--src/ninep/testing.zig2
-rw-r--r--src/pardes.zig5
-rw-r--r--src/tty/tty.zig15
8 files changed, 94 insertions, 0 deletions
diff --git a/src/detached/server.zig b/src/detached/server.zig
index a3694c57..0cf1729f 100644
--- a/src/detached/server.zig
+++ b/src/detached/server.zig
@@ -351,6 +351,7 @@ pub const Session = struct {
.pty_write = ptyWrite,
.pty_resize = ptyResize,
.pty_signal = ptySignal,
+ .close_pty = closePaneShell,
.tty_taken = ttyTaken,
.write_file = writeFile,
.write_dump = writeDump,
@@ -606,6 +607,11 @@ pub const Session = struct {
}
}
+ fn closePaneShell(ctx: ?*anyopaque, pane: u8) void {
+ const s = of(ctx);
+ if (pane < s.ptys.len) s.closePty(pane);
+ }
+
fn closePty(s: *Session, pane: u8) void {
const pt = &s.ptys[pane];
if (pt.fd < 0) return;
@@ -1229,6 +1235,44 @@ test "detached queued results preserve current requests and are discarded before
try std.testing.expect(s.core.lsp_wait == null);
}
+test "a detached session's terminal pane takes its shell with it when it closes" {
+ const gpa = std.testing.allocator;
+ var s: Session = .{
+ .gpa = gpa,
+ .worker_gpa = gpa,
+ .io = std.testing.io,
+ .core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }),
+ .cols = 40,
+ .rows = 12,
+ };
+ defer s.core.deinit();
+ defer s.deinit();
+ // A stand-in shell: it waits for a signal, and a hangup ends it.
+ const pid = libc.fork();
+ if (pid == 0) while (true) {
+ _ = libc.poll(&[0]libc.pollfd{}, 0, -1);
+ };
+ try std.testing.expect(pid > 0);
+ var fds: [2]c_int = undefined;
+ try std.testing.expectEqual(@as(c_int, 0), libc.pipe(&fds));
+ defer _ = libc.close(fds[1]);
+ s.ptys[3] = .{ .fd = fds[0], .pid = pid };
+ const host = s.host();
+ host.vtable.close_pty.?(host.ctx, 3);
+ try std.testing.expect(s.ptys[3].fd < 0);
+ const deadline = monotonicMs() + 2000;
+ while (monotonicMs() < deadline) {
+ s.harvest();
+ const running = s.ptys[3].pid != 0 or for (s.retired_shells) |shell| {
+ if (shell.pid == pid) break true;
+ } else false;
+ if (!running) break;
+ _ = libc.poll(&[0]libc.pollfd{}, 0, 5);
+ }
+ try std.testing.expect(s.ptys[3].pid == 0);
+ for (s.retired_shells) |shell| try std.testing.expect(shell.pid != pid);
+}
+
test "detached worker setup failure completes requests without changing document bytes" {
const gpa = std.testing.allocator;
var failing = std.testing.FailingAllocator.init(gpa, .{ .fail_index = 0 });
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 415b21e8..1d5d3c05 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -3756,6 +3756,10 @@ const Shell = struct {
};
}
+ fn closePaneShell(ctx: ?*anyopaque, pane: u8) void {
+ shellOf(ctx).closePty(pane);
+ }
+
fn closePty(s: *Shell, pane: u8) void {
const pt = if (s.ptys[pane]) |*pt| pt else return;
if (pt.fd < 0) return;
@@ -3831,6 +3835,7 @@ const Shell = struct {
.pty_write = ptyWrite,
.pty_resize = ptyResize,
.pty_signal = ptySignal,
+ .close_pty = closePaneShell,
.tty_taken = ttyTaken,
.write_file = writeFile,
.write_dump = writeDump,
diff --git a/src/host_io.zig b/src/host_io.zig
index 959ce3bd..a5efc7d1 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -20,6 +20,7 @@ pub const Host = struct {
pty_write: ?*const fn (ctx: ?*anyopaque, pane: u8, bytes: []const u8) void = null,
pty_resize: ?*const fn (ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void = null,
pty_signal: ?*const fn (ctx: ?*anyopaque, pane: u8, sig: pardes.PtySignal) void = null,
+ close_pty: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null,
tty_taken: ?*const fn (ctx: ?*anyopaque, pane: u8) bool = null,
gpio_toggle: ?*const fn (ctx: ?*anyopaque, pin: u16, was: *u8, now: *u8) bool = null,
write_file: ?*const fn (ctx: ?*anyopaque, pane: u8, path: []const u8, bytes: []const u8) void = null,
diff --git a/src/macos.zig b/src/macos.zig
index c9c83a2d..a0abc119 100644
--- a/src/macos.zig
+++ b/src/macos.zig
@@ -2310,6 +2310,7 @@ const vtable: pardes.Host.VTable = .{
.pty_write = ptyWrite,
.pty_resize = ptyResize,
.pty_signal = ptySignal,
+ .close_pty = closePty,
.tty_taken = ttyTaken,
.write_file = writeFile,
.write_dump = writeDump,
@@ -2501,6 +2502,15 @@ fn startReader(st: *State, pt: *Pty, id: u8) void {
};
}
+/// The pane is gone: its shell goes with it.
+fn closePty(ctx: ?*anyopaque, pane: u8) void {
+ const st = hostState(ctx);
+ const pt = st.ptys[pane] orelse return;
+ _ = libc.kill(pt.pid, posix.SIG.HUP);
+ reap(st, pane);
+ st.gens[pane] +%= 1;
+}
+
fn reap(st: *State, pane: u8) void {
var pt = st.ptys[pane] orelse return;
st.ptys[pane] = null;
diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig
index eda6e9d0..f788b4f8 100644
--- a/src/ninep/pty.zig
+++ b/src/ninep/pty.zig
@@ -806,6 +806,18 @@ test "a run's answer says cut when its output's start is gone, and reads a bound
try testing.expectEqualStrings("error out of memory\n", sh.answered(p, node, h, into));
}
+test "closing a terminal pane asks its shell to hang up the pty" {
+ const p = try withTerm(testing.allocator);
+ defer p.deinit();
+ _ = try th.newPane(p);
+ const closed = th.rmdir(p, Node.of(serialOf(p), .dir));
+ try testing.expectEqual(Status.ok, closed.reply.status);
+ try testing.expect(closed.closed);
+ // A text pane has no shell to end.
+ const text = try th.newPane(p);
+ try testing.expect(!th.rmdir(p, Node.of(text, .dir)).closed);
+}
+
test "the pty queue drops the oldest at its cap" {
const gpa = testing.allocator;
const p = try withTerm(gpa);
diff --git a/src/ninep/testing.zig b/src/ninep/testing.zig
index 6ea40956..fce77f10 100644
--- a/src/ninep/testing.zig
+++ b/src/ninep/testing.zig
@@ -19,6 +19,7 @@ pub const Answer = struct {
winsize: ?struct { cols: u16, rows: u16 } = null,
signal: ?pardes.PtySignal = null,
spawned: bool = false,
+ closed: bool = false,
pub fn pty(a: *const Answer) []const u8 {
return a.pty_buf[0..a.pty_len];
@@ -44,6 +45,7 @@ pub fn call(p: *Pardes, req: Req) Answer {
},
.resize_pty => |r| ans.winsize = .{ .cols = r.cols, .rows = r.rows },
.signal_pty => |s| ans.signal = s.sig,
+ .close_pty => ans.closed = true,
.spawn => ans.spawned = true,
else => {},
};
diff --git a/src/pardes.zig b/src/pardes.zig
index fbc3e119..442a3a30 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -3095,6 +3095,9 @@ pub const Effect = union(enum) {
write: struct { pane: u8, bytes: Buf(64) },
resize_pty: struct { pane: u8, cols: u16, rows: u16 },
signal_pty: struct { pane: u8, sig: PtySignal },
+ /// The terminal pane in this slot is gone: the shell hangs up its pty
+ /// and ends the shell, rather than leave it running with nobody to read.
+ close_pty: struct { pane: u8 },
open_link: Buf(256),
/// write this pane's file content to its path; the shell reads both off
/// the core (content is unbounded, effects are fixed-size values)
@@ -4129,6 +4132,7 @@ pub const Pardes = struct {
const watched = (if (pane.file) |f| f.output == null else false) or pane.hasPdf();
for (p.panes, 0..) |slot, id| if (slot == pane) {
if (watched) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } });
+ if (pane.isTerminal()) p.emit(.{ .close_pty = .{ .pane = @intCast(id) } });
ctlfs.events.noteRetire(p, pane);
// ...and so do bytes still queued for the pty it no longer has.
p.dropPendingWrite(id);
@@ -4522,6 +4526,7 @@ pub const Pardes = struct {
.write => |w| if (v.pty_write) |f| f(p.host.ctx, w.pane, w.bytes.slice()),
.resize_pty => |r| if (v.pty_resize) |f| f(p.host.ctx, r.pane, r.cols, r.rows),
.signal_pty => |s| if (v.pty_signal) |f| f(p.host.ctx, s.pane, s.sig),
+ .close_pty => |c| if (v.close_pty) |f| f(p.host.ctx, c.pane),
.open_link => |u| if (v.open_link) |f|
f(p.host.ctx, u.slice())
else
diff --git a/src/tty/tty.zig b/src/tty/tty.zig
index 903c5c0c..b2768ed6 100644
--- a/src/tty/tty.zig
+++ b/src/tty/tty.zig
@@ -924,6 +924,7 @@ const Shell = struct {
.pty_write = ptyWrite,
.pty_resize = ptyResize,
.pty_signal = ptySignal,
+ .close_pty = closePty,
.tty_taken = ttyTaken,
.write_file = writeFile,
.write_dump = writeDump,
@@ -1227,6 +1228,20 @@ const Shell = struct {
if (s.ptys[pane]) |pt| host_io.signalTty(pt.pid, pt.file.handle, sig);
}
+ /// The pane is gone: hang its pty up, which the kernel passes on to the
+ /// shell as SIGHUP, and say it too for a shell that ignores the tty's.
+ /// Output still in flight carries the old generation and is dropped.
+ fn closePty(ctx: ?*anyopaque, pane: u8) void {
+ const s = of(ctx);
+ var pt = s.ptys[pane] orelse return;
+ s.ptys[pane] = null;
+ s.gens[pane] +%= 1;
+ pt.reader.cancel(s.io) catch {};
+ _ = libc.close(pt.file.handle);
+ _ = libc.kill(pt.pid, posix.SIG.HUP);
+ _ = libc.waitpid(pt.pid, null, posix.W.NOHANG);
+ }
+
fn ttyTaken(ctx: ?*anyopaque, pane: u8) bool {
const s = of(ctx);
const pt = s.ptys[pane] orelse return false;