From f30ee05b0609b8f3aa488c94f3ff21733ed37053 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 28 Sep 2026 16:29:29 -0300 Subject: A command's exit is told once its output is in, by the pty's state, not a timer The watcher woke the host a second time 60 ms after the exit by its own clock while the grace was counted from the reap: a host busy for 10 ms missed it, and the tag said running for ever and Kill did nothing; and under load exit 0 could land before the last output. As decided, no timer: the exit is told once it is reaped and the pty says nothing is left (poll: no POLLIN, and no POLLHUP, which means the end of file is on its way behind the output), else at that end of file, checked after each chunk of output. The tty host checks inside its step so the frame shows it. The four hosts' copies are one host_io.takeExits/commandEof, which close a told command's pty at its end of file (the fd and the GUI's reader leaked when the exit came first). A finished command pane whose pty a job it left still holds is not reused, so that job is not hung up; and the reset before a reuse is SGR 0, not DECSTR, which ghostty's stream does not implement. Co-Authored-By: Claude Opus 5.5 --- src/detached/server.zig | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) (limited to 'src/detached/server.zig') diff --git a/src/detached/server.zig b/src/detached/server.zig index b47bda1b..8fc7b96e 100644 --- a/src/detached/server.zig +++ b/src/detached/server.zig @@ -484,7 +484,7 @@ pub const Session = struct { s.ninep, ) catch |err| return s.core.reportError(pane, "shell", err); const command = if (s.core.panes[pane]) |pn| pn.command != null else false; - s.ptys[pane] = .{ .fd = child.file.handle, .pid = child.pid, .cmd = .{ .watched = command and host_io.watchExit(child.pid) } }; + s.ptys[pane] = .{ .fd = child.file.handle, .pid = child.pid, .cmd = .{ .watched = command and host_io.watchExit(child.pid), .fd = child.file.handle } }; setNonblock(child.file.handle); var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; if (host_io.shellCwd(child.pid, &lbuf)) |wd| s.core.setCwd(pane, wd); @@ -697,11 +697,7 @@ pub const Session = struct { // 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) { - pt.cmd.eof = true; - s.core.update(.{ .eof = .{ .pane = pane } }); - return s.takeExits(); - } + if (pt.cmd.watched) return host_io.commandEof(s.core, &s.ptys, pane, s, closeWatched); // Unwatched, a command's exit is read here, as its end. const unwatched = if (s.core.panes[pane]) |pn| pn.command != null else false; var status: ?u8 = null; @@ -715,21 +711,13 @@ pub const Session = struct { s.core.update(.{ .eof = .{ .pane = pane } }); } - /// Each watched child that exited: reaped, the core told, and its pty - /// closed if its end of file came first. + /// The command panes' exits, told once their output is in (host_io). fn takeExits(s: *Session) void { - while (host_io.takeExited()) |pid| for (&s.ptys) |*pt| { - if (pt.fd < 0 or pt.pid != pid or pt.cmd.exited) continue; - pt.cmd.exit((host_io.reapExited(pid) orelse break).status); - pt.pid = 0; // reaped: no signal or retire may reach whoever gets it next - break; - }; - for (&s.ptys, 0..) |*pt, id| { - if (pt.fd < 0 or !pt.cmd.due()) continue; - pt.cmd.told = true; - s.core.update(.{ .exited = .{ .pane = @intCast(id), .status = pt.cmd.status } }); - if (pt.cmd.eof) s.closePty(@intCast(id)); - } + host_io.takeExits(s.core, &s.ptys, s, closeWatched); + } + + fn closeWatched(s: *Session, id: usize) void { + s.closePty(@intCast(id)); } fn harvest(s: *Session) void { -- cgit v1.3