diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 15:13:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 15:34:15 -0300 |
| commit | fb7e632248c599a70689cd0451392c8a890044ee (patch) | |
| tree | 8fafe9377885a50f2e49c0710f37c5268c980e42 /runtime | |
| parent | 0957cd7ad097c83a591e315f2e261996e72ea856 (diff) | |
| download | pardes-fb7e632248c599a70689cd0451392c8a890044ee.tar.gz pardes-fb7e632248c599a70689cd0451392c8a890044ee.zip | |
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/daemon.zig | 83 | ||||
| -rw-r--r-- | runtime/session.zig | 5 |
2 files changed, 75 insertions, 13 deletions
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). |
