summaryrefslogtreecommitdiff
path: root/src/daemon.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 20:56:03 -0300
committerGabriel Schneider <[email protected]>2026-10-30 03:13:15 -0300
commitfdf74e199de7d0fc648f912875323f3dc920ffc2 (patch)
tree7e175c6fc9cb653200ff2abb7bcc5cf0fda3dc9f /src/daemon.zig
parent57aa8d2f9ae851fa9806abbd1fd04abf1fd2b57f (diff)
downloadpardes-fdf74e199de7d0fc648f912875323f3dc920ffc2.tar.gz
pardes-fdf74e199de7d0fc648f912875323f3dc920ffc2.zip
replica clients over a socket: the session streams each its dump and every unit after it (replica.zig `Peers`, a writer and a reader thread a client, units at each step boundary, a client's input stepped there in its own view), and `pardes --attach=<socket>:<name>` and `--attach=<host>:<name>` (through `ssh -tt -L`) are a client in the tty host, its keys applied at once by its own replica and drawn by its own process
wire.zig version 15: welcome (the client's id and view, the dump's length, the session's UTC offset and digest), dump pieces, units (with the authority's clocks), terminal messages both ways, a check. A unit carries one digest, at its end (`unit_digests`). The client: tty.zig `Shell.remote`, `feed` (a terminal's input to the replica and the session); the session's clock guessed from the last unit's (tape.zig `Guess`); a pump woken only by a client's post reads no new time (`mail_only`), and a unit of another's ending where the last ended leaves the speculation as it is (`neutral`); the rollback copy taken while idle (`Replica.ready`); views a frontend here does not draw left undrawn. A lagging client never holds the session: its queue is cut off past 64 MiB (PARDES_REPLICA_LAG_KB). Detach in a client ends that client; a Restore lets every client go. daemon.zig: a replica the daemon hands over is the session's loop's (a note), not a stub thread's. A window session refuses one in words. test/daemon.py `--clients`: two clients (one through the delay relay) type into a detached session and agree with it, one killed outright, one detached, a third typing into a shell. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/daemon.zig')
-rw-r--r--src/daemon.zig61
1 files changed, 10 insertions, 51 deletions
diff --git a/src/daemon.zig b/src/daemon.zig
index 785f4afd..d2ae012c 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -357,19 +357,10 @@ pub fn adopt() void {
}
/// This session is about to exec another build (upgrade.zig): the daemon
-/// is asked to move too, and every replica told why it is let go.
-/// ponytail: a pong its thread writes at the same moment may split the
-/// words; the client reconnects either way.
+/// is asked to move too. Its replica clients were told why they are let go
+/// (replica.zig `Peers.refuseAll`).
pub fn upgraded() void {
tell("upgrade", &.{});
- var out: [600]u8 = undefined;
- const said = wire.encodeServer(&out, .{ .refused = "the session is upgrading to another build of pardes: connect again with that build" }) catch return;
- for (&replica_fds) |*slot| {
- const fd = slot.load(.acquire);
- if (fd < 0) continue;
- _ = writeAll(fd, said);
- _ = libc.shutdown(fd, 2);
- }
}
/// An upgraded session's link and stub as its old process left them: its
@@ -388,6 +379,8 @@ pub const Note = union(enum) {
/// stub's pipe; for a window session, both -1: open a window.
attach: struct { tty: c_int, stub: c_int },
adopted: struct { pids: [max_fds]libc.pid_t, fds: [max_fds]c_int, n: usize },
+ /// A replica's connection, its hello taken (src/replica.zig `Peers`).
+ replica: c_int,
};
/// Hands every note the daemon sends on to `deliver`, on a thread of its
@@ -416,9 +409,7 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no
} });
for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f);
} else if (std.mem.eql(u8, verb, "replica")) {
- if (got.nfds >= 1) {
- if (std.Thread.spawn(.{}, serveReplica, .{got.fds[0]})) |t| t.detach() else |_| _ = libc.close(got.fds[0]);
- }
+ if (got.nfds >= 1) deliver(ctx, .{ .replica = got.fds[0] });
for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f);
} else if (std.mem.eql(u8, verb, "adopted")) {
var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } };
@@ -450,43 +441,11 @@ fn writeAll(fd: c_int, bytes: []const u8) bool {
return true;
}
-var replica_fds: [max_replicas]std.atomic.Value(c_int) = @splat(.init(-1));
-
-/// A replica the daemon handed over, its hello accepted: welcomed, its pings
-/// answered, until it says bye or hangs up. ponytail: phase 5 proper makes it
-/// a replica (the log, the dump, terminal state); this stub is what times a
-/// round trip through the daemon's handoff and `ssh -L`. A message larger
-/// than its buffer ends it.
-fn serveReplica(fd: c_int) void {
- defer _ = libc.close(fd);
- // Where an upgrade finds it to say why it ends (`upgraded`).
- const slot = for (&replica_fds) |*r| {
- if (r.cmpxchgStrong(-1, fd, .acq_rel, .acquire) == null) break r;
- } else null;
- defer if (slot) |r| r.store(-1, .release);
- log.info("a replica connected to '{s}'", .{name()});
- defer log.info("a replica of '{s}' left", .{name()});
- var out: [wire.hello_max]u8 = undefined;
- const pid: u32 = @intCast(libc.getpid());
- if (!writeAll(fd, wire.encodeServer(&out, .{ .welcome = .{ .pid = pid, .session = name() } }) catch return)) return;
- var buf: [msg_max]u8 = undefined;
- var len: usize = 0;
- while (true) {
- while (wire.framed(buf[0..len]) catch return) |f| {
- switch (wire.decodeClient(f.tag, f.payload) catch return) {
- .ping => |v| if (!writeAll(fd, wire.encodeServer(&out, .{ .pong = v }) catch return)) return,
- .bye, .hello => return,
- .event => {},
- }
- std.mem.copyForwards(u8, &buf, buf[f.total..len]);
- len -= f.total;
- }
- if (len == buf.len) return;
- const n = libc.read(fd, buf[len..].ptr, buf.len - len);
- if (n < 0 and libc.errno(n) == .INTR) continue;
- if (n <= 0) return;
- len += @intCast(n);
- }
+/// A replica refused by a session that takes none (a window's, so far).
+pub fn refuseReplica(fd: c_int, why: []const u8) void {
+ var out: [600]u8 = undefined;
+ if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {}
+ _ = libc.close(fd);
}
/// A session ended by a signal (TERM, HUP, INT): its shells go with it, as