summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 14:27:42 -0300
committerGabriel Schneider <[email protected]>2026-10-29 14:27:42 -0300
commit791d0447aa2bdd4fe4ae0c76052e09acca4c61f2 (patch)
tree2c716d57babc0ac48faa8ae07387bcd84a9f4dca
parent45b4c9bb28596bb6102c387a4b0e4fbb87c78155 (diff)
downloadpardes-791d0447aa2bdd4fe4ae0c76052e09acca4c61f2.tar.gz
pardes-791d0447aa2bdd4fe4ae0c76052e09acca4c61f2.zip
daemon.zig: a forked session takes its stub's death, a SIGKILL too, as the terminal's HUP and detaches
PR_SET_PDEATHSIG (FreeBSD: PROC_PDEATHSIG_CTL) in the session child, cleared again at Detach so the stub's own exit then is no hangup; a stub dead before it was set hangs up at once. It is the HUP the signal thread already takes (blocked in every thread, sigwaited), and setsid does not touch it. An --attach stub is no parent of its session: one killed so still leaves the session drawing until the terminal closes or Detach. test/daemon.py kills a session's stub outright under a shell that keeps the terminal (on FreeBSD the session leader's exit revokes it), waits for the session to put the terminal back and detach, and attaches it again; without the death signal it times out. Co-Authored-By: Claude Opus 5.5 <[email protected]>
-rw-r--r--docs/next-daemon.md10
-rw-r--r--src/daemon.zig24
-rw-r--r--test/daemon.py28
3 files changed, 56 insertions, 6 deletions
diff --git a/docs/next-daemon.md b/docs/next-daemon.md
index f60e32ab..500b53fe 100644
--- a/docs/next-daemon.md
+++ b/docs/next-daemon.md
@@ -240,8 +240,11 @@ Decisions taken at the forks, with the ponytail lens:
session as before: in the terminal's own process, ended by its hangup,
for a harness that kills by pid and closes the terminal. A GUI test
window (`PARDES_TEST`) has none unless `PARDES_DAEMON=on`.
-- **Known gaps.** A stub killed with SIGKILL while its terminal stays open
- leaves the session drawing there until the terminal closes or Detach. A
+- **Known gaps.** A forked session takes its stub's death, a SIGKILL
+ too, as a HUP (PR_SET_PDEATHSIG; FreeBSD's PROC_PDEATHSIG_CTL) and
+ detaches; a `pardes --attach` stub is no parent of its session, and one
+ killed with SIGKILL while its terminal stays open still leaves the
+ session drawing there until the terminal closes or Detach. A
reattached terminal keeps the first one's environment (TERM, COLORTERM);
vaxis queries its capabilities again. A daemon killed outright leaves
its sessions unregistered, and Detach then refuses. A daemon of an older
@@ -378,7 +381,8 @@ Everything of ours lives in `/root/pardes-test`, nothing outside it.
- Off Linux, the parts of tests whose refusing path is Linux's
/proc/1 are skipped, and so is the builtins JSON check, since the
committed lists are Linux builds' (Tty9p included).
- - test/daemon.py passes there at 20 cycles. One earlier run failed at
+ - test/daemon.py passes there at 20 cycles, its killed stub included
+ (procctl's death signal). One earlier run failed at
cycle 3, one process short in its count (most likely ps -e failing to
read an environment).
- test/fs.py runs past its `pardes -` +Pager checks now, as far as the
diff --git a/src/daemon.zig b/src/daemon.zig
index c7729e59..0f770514 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -511,9 +511,25 @@ pub fn stubWait(session: libc.pid_t, child: bool, pipe: c_int) noreturn {
/// The session's half of its stub: what `stubWait` reads.
pub var stub: c_int = -1;
-/// Detached: the stub is told (and goes), and the pipe let go.
+extern "c" fn procctl(idtype: c_int, id: i64, cmd: c_int, data: ?*anyopaque) c_int;
+
+/// A HUP when this process's parent dies (a forked session's stub), or no
+/// longer. Linux keeps it per thread: set and cleared on the main one.
+fn hupOnStubDeath(on: bool) void {
+ var sig: c_int = if (on) @backingInt(posix.SIG.HUP) else 0;
+ switch (builtin.os.tag) {
+ .linux => _ = std.os.linux.prctl(@backingInt(std.os.linux.PR.SET_PDEATHSIG), @intCast(sig), 0, 0, 0),
+ // procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &sig)
+ .freebsd => _ = procctl(0, 0, 11, &sig),
+ else => {},
+ }
+}
+
+/// Detached: the stub is told (and goes), and the pipe let go. Its end is
+/// no hangup now (hupOnStubDeath).
pub fn tellStubDetached() void {
if (stub < 0) return;
+ hupOnStubDeath(false);
var buf: [160]u8 = undefined;
const said = std.fmt.bufPrint(&buf, "d{s}", .{name()}) catch "d";
_ = libc.write(stub, said.ptr, said.len);
@@ -549,6 +565,7 @@ pub fn forkSession(conn: c_int) ?void {
if (libc.pipe(&pipe) != 0) return null;
ninep_io.setCloexec(pipe[0]);
ninep_io.setCloexec(pipe[1]);
+ const parent = libc.getpid();
const pid = libc.fork();
if (pid < 0) {
_ = libc.close(pipe[0]);
@@ -558,6 +575,11 @@ pub fn forkSession(conn: c_int) ?void {
if (pid == 0) {
_ = libc.close(pipe[0]);
_ = libc.setsid();
+ // The stub's death, however it comes (a SIGKILL too), is a HUP:
+ // the terminal's end, which the session's signal thread takes and
+ // detaches on. One already dead hangs up at once.
+ hupOnStubDeath(true);
+ if (libc.getppid() != parent) _ = libc.raise(.HUP);
stub = pipe[1];
// The listener and lock are the daemon's, which the stub starts.
startPending(false);
diff --git a/test/daemon.py b/test/daemon.py
index f4f46286..9e9ea600 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -217,15 +217,39 @@ def main(binary, cycles):
time.sleep(.3)
assert alive(shell_pid), 'a killed session\'s shell is not hung up'
assert alive(daemon)
+ # Started from a shell, which leads the terminal's session and
+ # outlives the stub, as a terminal's shell does (on FreeBSD the
+ # leader's exit revokes the terminal for every process).
term = Term()
- stub = term.spawn([binary, '--tty', '--9p=fresh'], env)
- started.append(stub.pid)
+ stub_pid = root / 'stub.pid'
+ shell = term.spawn(['sh', '-c', '"$@" & echo $! > "$0"; wait; exec sleep 600',
+ str(stub_pid), binary, '--tty', '--9p=fresh'], env)
+ started.append(shell.pid)
sock = root / 'pardes-9p-fresh.sock'
wait_for('the second session', sock.exists)
+ stub = int(stub_pid.read_text())
+ started.append(stub)
client = Client(sock)
status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())
session = int(status['pid'])
started.append(session)
+ # Its stub killed outright, the terminal left open: the session
+ # (the stub's child) takes that as the terminal's end and detaches.
+ wait_for('the second session\'s first frame', lambda: b'\x1b[?1049h' in term.text())
+ drawn = len(term.text())
+ os.kill(stub, signal.SIGKILL)
+ wait_for('the stub gone', lambda: not alive(stub))
+ wait_for('the session detached from its killed stub', lambda: sized(client))
+ assert b'\x1b[?1049l' in term.text()[drawn:], 'the terminal is put back'
+ assert alive(session)
+ shell.kill()
+ shell.wait(timeout=10)
+ term.close()
+ term = Term()
+ stub = term.spawn([binary, '--attach=fresh'], env)
+ wait_for('the attached terminal\'s first frame', lambda: b'\x1b[?1049h' in term.text())
+ wait_for('the session sized to its new terminal', lambda: not sized(client))
+ print('daemon: a session whose stub was killed outright detached, and was attached again')
before = set(terminals(client))
client.write(f'/pane/{min(before)}/exec', b'Adopt\n')
adopted = wait_for('the adopted pane', lambda: set(terminals(client)) - before)