summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-23 04:25:32 -0300
committerGabriel Schneider <[email protected]>2026-11-23 07:28:22 -0300
commit100fe567edc8be7bea9b36ae7fbf20cc66d55164 (patch)
tree97595f87732de223559c56002f526d825d926eaa
parent396247178d20678a36e9e1782c2a457eeb1916de (diff)
downloadpardes-100fe567edc8be7bea9b36ae7fbf20cc66d55164.tar.gz
pardes-100fe567edc8be7bea9b36ae7fbf20cc66d55164.zip
replica: a client let go takes its notes out of the batch the session is taking, not only out of the next: its writer's `.joined` (its join written whole) could come after its reader's `.gone` in one boundary's batch (the writer descheduled between its last write and its post), `leave` -> `letGo` freed the peer, and the `.joined` after it read `w.peer.gen` off freed memory and, equal by chance, `released` someone else's pins (round 1 found it on the base; a user could hit it with a client that ends as its join's last bytes are written: a session crash or a heap overwrite in a release build); the failing test first: "a client's end and its writer's note after it, taken in one batch, let the client go once: nothing reads it freed" (the session's allocations a page each so a let-go peer is unmapped): on the base a segmentation fault at core/replica.zig:1978 in `boundary` (`.joined => |w| if (w.gen == w.peer.gen)`); the fix: `boundary` keeps its place in the batch (`Peers.taking`) and `letGo` takes the peer's notes out of the inbox and out of the batch after that place alike (`Peers.dropNotes`, the inbox's filter it had made one function); core-test 931/1095 (164 skipped: the merge's 930/1094 and this test), shell-test 1137/1140 (3 skipped); the whole gate g1 on this tree green (the report pardes-link-2.md)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
-rw-r--r--core/replica.zig61
1 files changed, 48 insertions, 13 deletions
diff --git a/core/replica.zig b/core/replica.zig
index 79abd977..6c4f0b2c 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -1833,7 +1833,10 @@ pub const Peers = struct {
seq: u32 = 0,
lock: std.Io.Mutex = .init,
inbox: std.ArrayListUnmanaged(Note) = .empty,
+ /// The batch `boundary` takes, and the note it is at: a client let go
+ /// there (`letGo`) takes its notes after that one out too.
taken: std.ArrayListUnmanaged(Note) = .empty,
+ taking: usize = 0,
/// A reader posted: the loop's one test a step while nothing is attached.
mail: std.atomic.Value(bool) = .init(false),
/// ...and what it posted was a client's input or a join, which the
@@ -1967,7 +1970,8 @@ pub const Peers = struct {
std.mem.swap(std.ArrayListUnmanaged(Note), &ps.inbox, &ps.taken);
ps.mail.store(false, .release);
ps.lock.unlock(ps.io);
- for (ps.taken.items) |note| switch (note) {
+ ps.taking = 0;
+ while (ps.taking < ps.taken.items.len) : (ps.taking += 1) switch (ps.taken.items[ps.taking]) {
.join => |j| ps.join(core, j),
.input => |in| {
ps.input(core, in.peer, in.msg);
@@ -2263,11 +2267,23 @@ pub const Peers = struct {
if (peer.want) |b| ps.gpa.free(b);
if (peer.z) |z| _ = zstd.ZSTD_freeCCtx(z);
peer.zout.deinit(ps.gpa);
- // What its reader posted after it was let go (its end) is nobody's.
+ // What it posted and is not taken yet is nobody's: its reader's
+ // after it was let go (its end), and its writer's last (`.joined`)
+ // in the next batch or after this note in the one taken now.
ps.lock.lockUncancelable(io);
- var i: usize = 0;
- while (i < ps.inbox.items.len) {
- const mine = switch (ps.inbox.items[i]) {
+ ps.dropNotes(&ps.inbox, 0, peer);
+ ps.lock.unlock(io);
+ ps.dropNotes(&ps.taken, ps.taking + 1, peer);
+ if (peer.rejoin) |j| ps.post(.{ .join = j });
+ ps.gpa.destroy(peer);
+ }
+
+ /// `peer`'s notes in `list` from `from` on taken out, in order (an
+ /// input's bytes freed).
+ fn dropNotes(ps: *Peers, list: *std.ArrayListUnmanaged(Note), from: usize, peer: *const Peer) void {
+ var i = from;
+ while (i < list.items.len) {
+ const mine = switch (list.items[i]) {
.input => |in| in.peer == peer,
.gone, .check, .again => |p| p == peer,
.joined => |w| w.peer == peer,
@@ -2277,15 +2293,9 @@ pub const Peers = struct {
i += 1;
continue;
}
- switch (ps.inbox.items[i]) {
- .input => |in| ps.gpa.free(in.msg),
- else => {},
- }
- _ = ps.inbox.orderedRemove(i);
+ if (list.items[i] == .input) ps.gpa.free(list.items[i].input.msg);
+ _ = list.orderedRemove(i);
}
- ps.lock.unlock(io);
- if (peer.rejoin) |j| ps.post(.{ .join = j });
- ps.gpa.destroy(peer);
}
/// Every client told the session is upgrading and let go, its view with
@@ -5681,6 +5691,31 @@ fn cutOff(core: *Pardes, ps: *Peers, peer: *Peer, link: *Link) !void {
try std.testing.expect(link.quit and !link.gone);
}
+test "a client's end and its writer's note after it, taken in one batch, let the client go once: nothing reads it freed" {
+ const gpa = std.testing.allocator;
+ const core = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" });
+ defer core.deinit();
+ core.host = TestHost.host();
+ try core.pump(core.host);
+ // The session's own allocations a page each: a client let go is
+ // unmapped, and a read of it after faults where it reads.
+ var ps: Peers = .{ .gpa = std.heap.page_allocator, .io = std.testing.io, .threads = false };
+ defer ps.deinit();
+ defer ps.dropAll(core);
+ var fd: c_int = -1;
+ const first = try MemoryLinks.join(&ps, core, null, &fd, null);
+ defer _ = libc.close(fd);
+ first.r.deinit();
+ ps.boundary(core);
+ // Its reader's end, then its writer's note that its join is written
+ // whole (the writer descheduled between its last write and its post):
+ // one boundary's batch.
+ ps.post(.{ .gone = first.peer });
+ ps.post(.{ .joined = .{ .peer = first.peer, .gen = first.peer.gen } });
+ ps.boundary(core);
+ try std.testing.expectEqual(@as(usize, 0), ps.count());
+}
+
test "a client cut off for what it left unread is told why and comes back over its own link: its goodbye and its hello there, the session's dump there in the view it has, no other connection; it loads it and agrees" {
const gpa = std.testing.allocator;
const core = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" });