summaryrefslogtreecommitdiff
path: root/runtime/daemon.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 02:21:52 -0300
committerGabriel Schneider <[email protected]>2026-11-20 04:00:32 -0300
commit1967cf64dc6d7490d386db874cf1866b764f235c (patch)
treefd11484ffcb69b6e70fe6ea9610e8fdfb8d682bf /runtime/daemon.zig
parent969369d4b84fa1847dc3dee0e737dcd58e56c5e7 (diff)
downloadpardes-1967cf64dc6d7490d386db874cf1866b764f235c.tar.gz
pardes-1967cf64dc6d7490d386db874cf1866b764f235c.zip
daemon: a session registers again with a daemon that was restarted: its link's reader, woken by the link's end alone (no timer, no poll: an idle session stays at 0 wakeups), dials the next daemon, starting one if none answers, and says its hello again on the link's own fd (dup3, so the session's own sends never race a new fd); the session loop, woken once by the note, tells it its shells' masters (`Ptys.tellDaemon`) and its clients again (`relinked`); a link the daemon never took (another build's) or the session's own end (`quit`) is not dialled again; test/daemon.py's `relink` (in plain, and alone as `--relink`), first failing on the base ("a daemon again" timed out: nothing dialled), now: the daemon killed, its two sessions (one detached, one with its terminal) start the next and are listed again with their shells, a client attaches, the detached one killed outright leaves its shell to another's Adopt, 0 and 0 wakeups in 2 s before and after; and the killed-session words in daemon.py read once the test's terminal has them (a race of the test's own); gated with the previous change, Debug: tui core-test 855/1005, shell-test 1046/1049, term 17/17, pane 183/184, kitty 45/45, behaviour 275/275 and its replay, process-test 0 of 144 failed, both soaks diverged 0 unequal 0 at 30/80/207 ms (corpus 22 scenes the same), daemon.py plain (50 cycles), far, games, window-clients, gui, upgrade both ways with clients attached pass; gui unit-test 1643/1647 (BaselineHarnessMismatch only); macOS x86_64 and aarch64, FreeBSD cross-builds pass; the frame and perf gate against tree2 PASS: 77/77 frames, perfab-corpus2x -0.114% (threshold 0.4%), 121 per-operation counts within 0.1% (largest +0.014%)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'runtime/daemon.zig')
-rw-r--r--runtime/daemon.zig124
1 files changed, 87 insertions, 37 deletions
diff --git a/runtime/daemon.zig b/runtime/daemon.zig
index 1b9149b6..80e1cc09 100644
--- a/runtime/daemon.zig
+++ b/runtime/daemon.zig
@@ -12,7 +12,8 @@
//! - whether a terminal or a window shows it (`state`).
//! It lists the sessions (`pardes ps`), hands a crashed session's masters
//! to the one that asks for them (Adopt), and a client's connection to the
-//! session it names (below).
+//! session it names (below). A session whose daemon died dials the next
+//! one and registers again (`readNotes`).
//!
//! Messages are SOCK_SEQPACKET datagrams: a line of words, and the fds it
//! carries (SCM_RIGHTS).
@@ -290,9 +291,17 @@ fn hello(fd: c_int, k: Kind, pid: libc.pid_t, named: []const u8, detached: bool)
/// tell of their ptys; -1 with none (no daemon, or PARDES_DAEMON=off).
pub var link: c_int = -1;
/// The daemon said `ok` to the hello, and the link has not ended since;
-/// `gone` once it has ended.
+/// `gone` once it has ended for good (`readNotes`).
var up = std.atomic.Value(bool).init(false);
var gone = std.atomic.Value(bool).init(false);
+/// The session is ending (`quit`): its link's end is no daemon's death.
+var quitting = std.atomic.Value(bool).init(false);
+/// A new daemon took the hello since the last one died: the rest of what
+/// the dead one was told is to be told again (`relinked`).
+var retell = std.atomic.Value(bool).init(false);
+/// What the hello says besides the name: the kind, and whether detached.
+var kind: Kind = .tty;
+var detached_now = std.atomic.Value(bool).init(false);
/// What it is called (its 9P name).
var name_buf: [128]u8 = undefined;
var name_len: usize = 0;
@@ -328,6 +337,8 @@ fn setName(named: []const u8) void {
pub fn register(fd: ?c_int, k: Kind, named: []const u8, detached: bool) bool {
if (comptime !supported) return false;
setName(named);
+ kind = k;
+ detached_now.store(detached, .release);
const c = fd orelse connect(true) orelse return false;
startPending(true);
if (!hello(c, k, libc.getpid(), name(), detached)) {
@@ -362,19 +373,33 @@ pub fn closed(pid: libc.pid_t) void {
/// Its own frontend let go (Detach, its terminal's end): detached for
/// good, an attach after it a client.
pub fn setDetached() void {
+ detached_now.store(true, .release);
tell("state detached", &.{});
}
+/// The last `clients` line, kept to be told to a new daemon (`relinked`).
+var clients_line: ["clients".len + max_clients * " -2147483648".len]u8 = undefined;
+var clients_len: usize = 0;
+
/// This session's replica clients now, each its process's pid (0 when its
/// socket's peer is not known), for `--ps`.
pub fn clients(pids: []const libc.pid_t) void {
- var buf: ["clients".len + max_clients * " -2147483648".len]u8 = undefined;
- var w: std.Io.Writer = .fixed(&buf);
- w.writeAll("clients") catch unreachable; // a literal, its .len in buf's
- for (pids[0..@min(pids.len, max_clients)]) |pid| w.print(" {d}", .{pid}) catch unreachable; // max_clients i32s, in buf's type
+ var w: std.Io.Writer = .fixed(&clients_line);
+ w.writeAll("clients") catch unreachable; // a literal, its .len in clients_line's
+ for (pids[0..@min(pids.len, max_clients)]) |pid| w.print(" {d}", .{pid}) catch unreachable; // max_clients i32s, in clients_line's type
+ clients_len = w.end;
tell(w.buffered(), &.{});
}
+/// Whether a new daemon took this session's hello since the last call
+/// (the session loop's, each pass): its clients told it again here, its
+/// ptys the caller's to tell (`pty`).
+pub fn relinked() bool {
+ if (!retell.swap(false, .acq_rel)) return false;
+ if (clients_len > 0) tell(clients_line[0..clients_len], &.{});
+ return true;
+}
+
/// Asks for the masters of sessions that died holding them; they come back
/// as an `adopted` note.
pub fn adopt() void {
@@ -396,10 +421,15 @@ pub fn resumeLink(fd: c_int, stub_fd: c_int, k: Kind, named: []const u8, detache
stub = stub_fd;
if (stub >= 0) hupOnStubDeath(true);
setName(named);
+ kind = k;
+ detached_now.store(detached, .release);
if (fd >= 0 and hello(fd, k, libc.getpid(), name(), detached)) link = fd else if (fd >= 0) _ = libc.close(fd);
}
pub const Note = union(enum) {
+ /// A new daemon took the hello (`readNotes`): the session loop is woken
+ /// to tell it the rest (`relinked`).
+ relinked,
adopted: struct { pids: [max_fds]libc.pid_t, fds: [max_fds]c_int, n: usize },
/// A replica's connection, its hello taken (core/replica.zig `Peers`),
/// and the view it joins again for, if it does.
@@ -417,39 +447,58 @@ pub fn watch(ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) v
fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void {
var buf: [msg_max]u8 = undefined;
- // The end of the link (a daemon gone, a hello refused): not kept.
- defer {
- up.store(false, .release);
- gone.store(true, .release);
- }
- while (recv(fd, &buf)) |got| {
- var words = std.mem.tokenizeScalar(u8, got.words, ' ');
- const verb = words.next() orelse "";
- if (std.meta.stringToEnum(enum { ok, replica, adopted }, verb)) |v| switch (v) {
- .ok => up.store(true, .release),
- .replica => {
- const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null;
- if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } });
- for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f);
- },
- .adopted => {
- var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } };
- const a = &note.adopted;
- for (got.fds[0..got.nfds]) |f| {
- const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch {
- _ = libc.close(f);
- continue;
- };
- a.pids[a.n] = pid;
- a.fds[a.n] = f;
- a.n += 1;
- }
- deliver(ctx, note);
- },
- } else for (got.fds[0..got.nfds]) |f| {
- _ = libc.close(f);
+ while (true) {
+ while (recv(fd, &buf)) |got| {
+ var words = std.mem.tokenizeScalar(u8, got.words, ' ');
+ const verb = words.next() orelse "";
+ if (std.meta.stringToEnum(enum { ok, replica, adopted }, verb)) |v| switch (v) {
+ .ok => up.store(true, .release),
+ .replica => {
+ const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null;
+ if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } });
+ for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f);
+ },
+ .adopted => {
+ var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } };
+ const a = &note.adopted;
+ for (got.fds[0..got.nfds]) |f| {
+ const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch {
+ _ = libc.close(f);
+ continue;
+ };
+ a.pids[a.n] = pid;
+ a.fds[a.n] = f;
+ a.n += 1;
+ }
+ deliver(ctx, note);
+ },
+ } else for (got.fds[0..got.nfds]) |f| {
+ _ = libc.close(f);
+ }
}
+ // The end of the link. One whose daemon took the hello (it died, or
+ // let the session go) is dialled again, a daemon started if none
+ // answers, and the hello said, here, off the session's way: the
+ // link's end is what wakes this, never a timer. One that never took
+ // it (another build's daemon), or the session's own end, is not:
+ // an end begun while it dialled finds the new link (`quit`).
+ if (!up.swap(false, .acq_rel) or quitting.load(.acquire) or !redial(fd) or quitting.load(.acquire)) break;
+ retell.store(true, .release);
+ deliver(ctx, .relinked);
}
+ // Not kept, until the session's end.
+ gone.store(true, .release);
+}
+
+/// A new connection in the place of `fd`, the link (its number kept, so
+/// the session's own sends never race a new fd), and the hello said on it.
+fn redial(fd: c_int) bool {
+ if (comptime !supported) return false;
+ const c = connect(true) orelse return false;
+ startPending(true);
+ defer _ = libc.close(c);
+ return libc.dup3(c, fd, @bitCast(@as(u32, @bitCast(libc.O{ .CLOEXEC = true })))) >= 0 and
+ hello(fd, kind, libc.getpid(), name(), detached_now.load(.acquire));
}
/// `bytes` sent whole on a socket, never raising SIGPIPE: false when the
@@ -485,6 +534,7 @@ pub fn hangUp() void {
/// link closed, the reader joined.
pub fn quit() void {
if (link < 0) return;
+ quitting.store(true, .release);
tell("quit", &.{});
_ = libc.shutdown(link, 2);
if (reader) |r| r.join();