From e9c66f134f359f7b50dd9273097ffb47166d251e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:59:29 -0300 Subject: gui: a pty slot never holds a closed fd (closePty empties the slot in every path), so the fd guards on it, its kill_at, reap's pass over the ptys and shutdownPtys' kill of ptys stopPtys has already emptied go Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/gui/gui.zig | 47 ++++++++++------------------------------------- 1 file changed, 10 insertions(+), 37 deletions(-) (limited to 'src/gui/gui.zig') diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 36850377..56782f7a 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -829,7 +829,6 @@ const Pty = struct { fd: c_int, pid: libc.pid_t, serial: u32, - kill_at: i64 = 0, /// 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); @@ -3816,10 +3815,6 @@ const Shell = struct { fn reap(s: *Shell) void { const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); for (&s.retired_shells) |*child| host_io.reapRetired(&child.pid, &child.kill_at, now); - for (s.ptys) |*slot| if (slot.*) |*pt| if (pt.fd < 0) { - host_io.reapRetired(&pt.pid, &pt.kill_at, now); - if (pt.pid == 0) slot.* = null; - }; } fn closePaneShell(ctx: ?*anyopaque, pane: u8) void { @@ -3828,7 +3823,6 @@ const Shell = struct { fn closePty(s: *Shell, pane: u8) void { const pt = if (s.ptys[pane]) |*pt| pt else return; - if (pt.fd < 0) 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(pane, s.gens[pane]); @@ -3837,25 +3831,21 @@ const Shell = struct { pt.reader = null; } _ = libc.close(pt.fd); - pt.fd = -1; daemon.closed(pt.pid); + var pid = pt.pid; + s.ptys[pane] = null; const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); - host_io.reapRetired(&pt.pid, &pt.kill_at, now); - if (pt.pid == 0) { - s.ptys[pane] = null; - return; - } - _ = libc.kill(pt.pid, libc.SIG.HUP); - pt.kill_at = now + 100; + var kill_at: i64 = 0; + host_io.reapRetired(&pid, &kill_at, now); + if (pid == 0) return; + _ = libc.kill(pid, libc.SIG.HUP); for (&s.retired_shells) |*child| if (child.pid == 0) { - child.* = .{ .pid = pt.pid, .kill_at = pt.kill_at }; - s.ptys[pane] = null; + child.* = .{ .pid = pid, .kill_at = now + 100 }; return; }; // No room to wait for it here: a slot kept for it would refuse the // next shell spawned into this pane until it was reaped. - host_io.retireShell(pt.pid); - s.ptys[pane] = null; + host_io.retireShell(pid); } fn stopPtys(s: *Shell) void { @@ -3869,23 +3859,15 @@ const Shell = struct { for (s.retired_shells) |child| if (child.pid > 0) { _ = libc.kill(child.pid, libc.SIG.KILL); }; - for (s.ptys) |slot| if (slot) |pt| if (pt.pid > 0) { - _ = libc.kill(pt.pid, libc.SIG.KILL); - }; for (&s.retired_shells) |*child| if (child.pid > 0) { while (libc.waitpid(child.pid, null, 0) < 0 and libc.errno(-1) == .INTR) {} child.* = .{}; }; - for (s.ptys) |*slot| if (slot.*) |pt| { - while (libc.waitpid(pt.pid, null, 0) < 0 and libc.errno(-1) == .INTR) {} - slot.* = null; - }; } fn reconcilePtys(s: *Shell) void { s.reap(); for (s.ptys, 0..) |slot, pane| if (slot) |pt| { - if (pt.fd < 0) continue; const current = s.core.panes[pane]; if (current == null or current.?.serial != pt.serial or !current.?.isTerminal()) s.closePty(@intCast(pane)); @@ -4323,8 +4305,6 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // shows it, and it is caught up at once when the window is back. const retiring = for (s.retired_shells) |child| { if (child.kill_at != 0) break true; - } else for (s.ptys) |slot| { - if (slot) |pt| if (pt.kill_at != 0) break true; } else false; const polls = g.scroll_lag != 0 or g.scroll_delta != 0 or g.scroll_notch != 0 or g.glide.moving() or g.gamepad != null or s.test_mode or core.needs_frame or retiring or g.post.animating(core.settings.shader_animation); @@ -4861,15 +4841,12 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { const s = shellOf(ctx); - if (s.ptys[pane]) |pt| if (pt.fd >= 0) { - _ = host_io.writeFd(pt.fd, bytes); - }; + if (s.ptys[pane]) |pt| _ = host_io.writeFd(pt.fd, bytes); } fn ptyResize(ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void { const s = shellOf(ctx); if (s.ptys[pane]) |pt| { - if (pt.fd < 0) return; const ws = host_io.ptyWinsize(s.core, cols, rows); _ = posix.system.ioctl(pt.fd, TIOCSWINSZ, @intFromPtr(&ws)); } @@ -4878,27 +4855,24 @@ fn ptyResize(ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void { fn ptySignal(ctx: ?*anyopaque, pane: u8, sig: pardes.PtySignal) void { 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.fd >= 0 and !pt.cmd.exited) host_io.signalTty(pt.pid, pt.fd, sig, pt.cmd.watched); + if (s.ptys[pane]) |pt| if (!pt.cmd.exited) host_io.signalTty(pt.pid, pt.fd, sig, pt.cmd.watched); } fn ttyTaken(ctx: ?*anyopaque, pane: u8) bool { const s = shellOf(ctx); const pt = s.ptys[pane] orelse return false; - if (pt.fd < 0) return false; return host_io.ttyTaken(pt.pid, pt.fd); } fn fgName(ctx: ?*anyopaque, pane: u8, buf: []u8) ?[]const u8 { const s = shellOf(ctx); const pt = s.ptys[pane] orelse return null; - if (pt.fd < 0) return null; return host_io.foregroundName(pt.pid, pt.fd, buf); } fn killJob(ctx: ?*anyopaque, pane: u8) bool { const s = shellOf(ctx); const pt = s.ptys[pane] orelse return false; - if (pt.fd < 0) return false; return host_io.killJob(pt.pid, pt.fd); } @@ -5069,7 +5043,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| { - if (pt.fd < 0) continue; 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(id, cwd); }; -- cgit v1.3