From fb7e632248c599a70689cd0451392c8a890044ee Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 15:13:48 -0300 Subject: daemon: the reader hands no note to a session loop that ended: it took the loop's address once, at its start, and ran on until main's daemon.quit joined it, after Loop.run returned, so a client connecting (or a shell adopted, or a daemon relinked) while a session ended was handed to the loop's dead frame (l.peers, l.inbox, deinit'ed); now the reader hands each note under a lock to where notesTo says (daemon.give), the session loop sets it after its frontend opened and clears it (notesTo(null)) before its peers, frontend and queue go, and a note with nowhere to go is let go, its connections closed; test "a client's connection the daemon hands on after the session's loop ended reaches no loop: it is closed" (a fake daemon on a socketpair link): "expected 0, found 1" (the ended loop handed it) on the base, passes now; a user could hit it: a pardes attach (or Adopt's answer) in the moment its session exits: a write into a dead stack frame (a crash or a corrupted teardown) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- runtime/daemon.zig | 83 +++++++++++++++++++++++++++++++++++++++++++++-------- runtime/session.zig | 5 +++- 2 files changed, 75 insertions(+), 13 deletions(-) (limited to 'runtime') diff --git a/runtime/daemon.zig b/runtime/daemon.zig index 9b566e60..453ee101 100644 --- a/runtime/daemon.zig +++ b/runtime/daemon.zig @@ -307,8 +307,11 @@ var detached_now = std.atomic.Value(bool).init(false); var name_buf: [128]u8 = undefined; var name_len: usize = 0; var reader: ?std.Thread = null; -/// Where the reader hands each note (`notesTo`). -var notes: ?struct { ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void } = null; +/// Where the reader hands each note (`notesTo`), the lock a hand-over and +/// a change of it take. +var notes: ?Notes = null; +var notes_lock: std.Io.Mutex = .init; +pub const Notes = struct { ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void }; pub fn name() []const u8 { return name_buf[0..name_len]; @@ -452,20 +455,39 @@ pub const Note = union(enum) { pub const Joining = struct { fd: c_int, view: ?u8 = null }; /// Where every note the daemon sends goes, once `watch` has started the -/// reader: the session's, before its first step. -pub fn notesTo(ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void { - notes = .{ .ctx = ctx, .deliver = deliver }; +/// reader: the session loop's, before its first step, until its state +/// goes (null): once this returns, no note reaches the one before. +pub fn notesTo(to: ?Notes) void { + const io = std.Io.Threaded.global_single_threaded.io(); + notes_lock.lockUncancelable(io); + defer notes_lock.unlock(io); + notes = to; +} + +/// A note handed on (`notesTo`); with nowhere to go (the loop ended, the +/// link not yet), let go: its connections closed. +fn give(note: Note) void { + const io = std.Io.Threaded.global_single_threaded.io(); + notes_lock.lockUncancelable(io); + defer notes_lock.unlock(io); + if (notes) |n| return n.deliver(n.ctx, note); + switch (note) { + .relinked => {}, + .adopted => |a| for (a.fds[0..a.n]) |f| { + _ = libc.close(f); + }, + .replica => |j| _ = libc.close(j.fd), + } } /// Hands every note the daemon sends on (`notesTo`), on a thread of its /// own, until the link ends (`quit`). Called each loop: started once. pub fn watch() void { - const n = notes orelse return; - if (link < 0 or reader != null) return; - reader = std.Thread.spawn(.{}, readNotes, .{ link, n.ctx, n.deliver }) catch null; + if (notes == null or link < 0 or reader != null) return; + reader = std.Thread.spawn(.{}, readNotes, .{link}) catch null; } -fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void { +fn readNotes(fd: c_int) void { var buf: [msg_max]u8 = undefined; while (true) { while (recv(fd, &buf)) |got| { @@ -475,7 +497,7 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no .ok => settle(.up), .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 } }); + if (got.nfds >= 1) give(.{ .replica = .{ .fd = got.fds[0], .view = view } }); for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); }, .adopted => { @@ -490,7 +512,7 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no a.fds[a.n] = f; a.n += 1; } - deliver(ctx, note); + give(note); }, } else for (got.fds[0..got.nfds]) |f| { _ = libc.close(f); @@ -504,7 +526,7 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no // an end begun while it dialled finds the new link (`quit`). if (answer.swap(.asked, .acq_rel) != .up or quitting.load(.acquire) or !redial(fd) or quitting.load(.acquire)) break; retell.store(true, .release); - deliver(ctx, .relinked); + give(.relinked); } // Not kept, until the session's end. settle(.gone); @@ -1204,6 +1226,43 @@ pub fn serve(state: ?[]const u8) noreturn { libc._exit(0); } +test "a client's connection the daemon hands on after the session's loop ended reaches no loop: it is closed" { + if (comptime !supported) return error.SkipZigTest; + var pair: [2]c_int = undefined; + try std.testing.expectEqual(@as(c_int, 0), libc.socketpair(libc.AF.UNIX, libc.SOCK.SEQPACKET | libc.SOCK.CLOEXEC, 0, &pair)); + defer _ = libc.close(pair[1]); + // The session's link, its daemon this test's end. + link = pair[0]; + defer { + quit(); + quitting.store(false, .release); + answer.store(.asked, .release); + } + const Loop = struct { + var notes_taken: u32 = 0; + fn note(_: *anyopaque, n: Note) void { + notes_taken += 1; + if (n == .replica) _ = libc.close(n.replica.fd); + } + }; + var loop: u8 = 0; + notesTo(.{ .ctx = &loop, .deliver = Loop.note }); + watch(); + try std.testing.expect(send(pair[1], "ok", &.{})); + // The loop ends (runtime/session.zig `run`); the link outlives it + // until main's `quit`. + notesTo(null); + var conn: [2]c_int = undefined; + try std.testing.expectEqual(@as(c_int, 0), libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &conn)); + defer _ = libc.close(conn[0]); + try std.testing.expect(send(pair[1], "replica 0", &.{conn[1]})); + _ = libc.close(conn[1]); + // Closed by the reader: the client sees its link end. + var fds = [_]posix.pollfd{.{ .fd = conn[0], .events = posix.POLL.IN, .revents = 0 }}; + try std.testing.expectEqual(@as(usize, 1), try posix.poll(&fds, 5000)); + try std.testing.expectEqual(@as(u32, 0), Loop.notes_taken); +} + test "a message carries its words and its fds, and the peer's end is null" { if (comptime !supported) return error.SkipZigTest; var pair: [2]c_int = undefined; diff --git a/runtime/session.zig b/runtime/session.zig index 9c8446e3..a0c27799 100644 --- a/runtime/session.zig +++ b/runtime/session.zig @@ -101,10 +101,13 @@ pub fn Loop(comptime Frontend: type) type { l.host = try fe.open(&l); defer fe.close(&l); - daemon.notesTo(&l, note); defer l.peers.deinit(); // Before the core goes: each client sees its link end. defer l.peers.dropAll(l.core); + // The daemon's notes, to the loop until its state goes (the + // reader runs on until main's `daemon.quit`). + daemon.notesTo(.{ .ctx = &l, .deliver = note }); + defer daemon.notesTo(null); // A first client on the socket it was started with, before its // first effects: it has every terminal from its first byte, and // no reload (Core.openView). -- cgit v1.3 From d720cf44d458e3986d371f4b8b768a97ba489c9c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 15:13:48 -0300 Subject: ninep: a retry of a held Edit write is answered by the listener from its hold, no step: serveFsInner's first lines read fs.edit_hold, which the session's listener sets between steps (9p_io), so a retried write was the session's step that answered it and every replica's step that ran the Edit again (the tape's divergence, and a client's second pipe); the check is EditHold.retried, which the listener calls before serveFs, and the step never reads the hold (fs.edit_hold stays the listener's, as the namespace's doc now says); test "a client runs an Edit a script's write started once, as its session does, when the held write is asked again" (core/replica.zig, a client joined, the write held as the listener holds it, its command answered, the retry as the listener serves it): diverged on the base ("the core called clock where the tape has a done (0), in serve_fs"), passes now; edit_cmd's held-write test retries through EditHold.retried; a user could hit it: a script's Edit with a | < or > command whose write's park is retried (a 9P client's retry while the command runs) under a far or window client: the client diverged and joined again Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 6 ------ core/edit_cmd.zig | 4 ++-- core/fs.zig | 17 +++++++++++++---- core/replica.zig | 25 +++++++++++++++++++++++++ runtime/9p_io.zig | 2 +- 5 files changed, 41 insertions(+), 13 deletions(-) (limited to 'runtime') diff --git a/core/Core.zig b/core/Core.zig index ff704bc6..98e2e407 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1571,12 +1571,6 @@ fn stepInner(p: *Pardes) void { /// local socket costs microseconds and not a vsync. pub const serveFs = pardes.tape.taped(serveFsInner); fn serveFsInner(p: *Pardes, req: ctlfs.Req) ctlfs.Reply { - // The held write of an Edit whose commands run, asked again by a - // retry of its park: still waiting, or its answer, never run twice. - if (p.fs.edit_hold) |*h| if (h.names(req)) { - if (!h.done) return .{ .tag = req.tag, .status = .again }; - return filesystem.EditHold.answer(&p.fs); - }; defer pardes.panes.File.syncTwins(p); if (!ctlfs.changesPane(p, req)) return ctlfs.handle(p, req); // An address, a limit or the mark flag is nothing drawn: a script's diff --git a/core/edit_cmd.zig b/core/edit_cmd.zig index 9a5c6865..ab4f2b5d 100644 --- a/core/edit_cmd.zig +++ b/core/edit_cmd.zig @@ -533,11 +533,11 @@ test "the held write asked again is parked again, then answered once, and a flus // As 9p_io holds it. p.fs.edit_hold = .{ .asker = @ptrCast(p), .slot = 0, .seq = 1, .tag = 7, .node = ctl, .handle = 0, .written = @intCast(req.data.len) }; // A retry of its park: still waiting, never run a second time. - try testing.expectEqual(tree.Status.again, p.serveFs(req).status); + try testing.expectEqual(tree.Status.again, filesystem.EditHold.retried(&p.fs, req).?.status); try testing.expect(p.pipe.edit_run != null); answer(p, &.{"a\nb\n"}); try testing.expect(p.fs.edit_hold.?.done); - const done = p.serveFs(req); + const done = filesystem.EditHold.retried(&p.fs, req).?; try testing.expectEqual(tree.Status.ok, done.status); try testing.expectEqual(@as(u32, @intCast(req.data.len)), done.written); try testing.expect(p.fs.edit_hold == null); diff --git a/core/fs.zig b/core/fs.zig index eadb7c19..a64487d6 100644 --- a/core/fs.zig +++ b/core/fs.zig @@ -830,6 +830,16 @@ pub const EditHold = struct { return req.op == .write and req.tag == h.tag and req.node == h.node and req.handle == h.handle; } + /// The held write asked again by a retry of its park, as the listener + /// answers it, never a step (no replica holds it): still waiting, or + /// its answer; null for any other request. + pub fn retried(ns: *Namespace, req: tree.Req) ?tree.Reply { + const h = if (ns.edit_hold) |*held| held else return null; + if (!h.names(req)) return null; + if (!h.done) return .{ .tag = req.tag, .status = .again }; + return answer(ns); + } + /// Its answer, the held record let go: the words copied out first. pub fn answer(ns: *Namespace) tree.Reply { const h = ns.edit_hold.?; @@ -852,9 +862,8 @@ pub const Namespace = struct { /// the server's (core/walk.zig): its sockets and the mounts (dumped /// on their own), the log and what a reader or a listener is answered /// with, and the request being served's own flags, set and read in - /// its step. ponytail: `edit_hold`, set by the session's listener - /// between steps and read by the retried write's step, is no - /// replica's (reports/pardes-faults-4.md). + /// its step, and `edit_hold`: the listener's, which answers a retry + /// of the held write from it, no step (`EditHold.retried`). pub const replica = .{ .authority = .{ "socket_path", "tcp_address", "mounts", "node_name", "os_paths", "out", @@ -932,7 +941,7 @@ pub const Namespace = struct { /// commands run off the loop (edit_cmd.zig): the listener holds it. edit_started: bool = false, /// That write, held: answered once `done` (9p_io answerHeld), or - /// asked again by a retry of its park (Pardes.serveFs); a flush or a + /// asked again by a retry of its park (`EditHold.retried`); a flush or a /// hang-up stops the Edit (9p_io watchEdit). edit_hold: ?EditHold = null, /// The words a held Edit write was refused with, kept for its answer. diff --git a/core/replica.zig b/core/replica.zig index ff1e138e..f3269c84 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -7364,6 +7364,31 @@ test "a client joined while a script's writes to a body are held to go in as one try n.agree(); } +test "a client runs an Edit a script's write started once, as its session does, when the held write is asked again" { + var n = try NinepJoin.init("b\na\n", TestHost.host()); + defer n.deinit(); + try n.join(); + const ctl = n.node(.ctl); + const req: pardes.ctlfs.Req = .{ .tag = 7, .op = .write, .node = ctl, .data = "Edit , | sort\n" }; + // The step alone: its loop's pass would run the command (here, with no + // host to run it, fail it). + const first = try n.serve(req, false); + try std.testing.expectEqual(pardes.ctlfs.Status.ok, first.status); + // The write held as the session's listener holds it (runtime/9p_io.zig). + n.a.core.fs.edit_hold = .{ .asker = @ptrCast(n.a.core), .slot = 0, .seq = 1, .tag = req.tag, .node = ctl, .handle = 0, .written = first.written }; + // Its command answers: the Edit is done. + n.a.apply(.{ .pipe_resp = .{ .id = n.a.core.pipe.edit_run.?.id, .success = true, .outputs = &.{"a\nb\n"} } }); + n.a.seq += 1; + try n.send(try n.a.unit(0, n.a.seq)); + try n.agree(); + // A retry of the held write's park, as the listener serves it: its + // answer from the hold, no step, the Edit not run again by anyone. + const again = pardes.filesystem.EditHold.retried(&n.a.core.fs, req) orelse try n.serve(req, true); + try std.testing.expectEqual(pardes.ctlfs.Status.ok, again.status); + try std.testing.expect(n.a.core.pipe.edit_run == null); + try n.agree(); +} + /// A session with a shell, for a run's 9P state across a join: a host /// whose pty takes the line and whose tty nobody else holds. const RunJoin = struct { diff --git a/runtime/9p_io.zig b/runtime/9p_io.zig index aaca377d..1079b102 100644 --- a/runtime/9p_io.zig +++ b/runtime/9p_io.zig @@ -203,7 +203,7 @@ pub const Listener = struct { // Asked first: a release that runs a held line has none after. const changes = pardes.ctlfs.changesPane(core, req); const fills = pardes.ctlfs.handsOff(core, req); - const reply = core.serveFs(req); + const reply = pardes.filesystem.EditHold.retried(&core.fs, req) orelse core.serveFs(req); const edit_started = core.fs.edit_started; core.fs.edit_started = false; // The held Edit write asked again by a retry, its Edit still -- cgit v1.3