diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-19 13:16:15 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-19 15:09:19 -0300 |
| commit | 553a62bac675bfc0a64254718488d3c8012f533e (patch) | |
| tree | bf715815347a72486ccdffeb9a5d3c08dedac76b /runtime/main.zig | |
| parent | 64b8951d198aa0dcd0a544abd6f103693b5205aa (diff) | |
| download | pardes-553a62bac675bfc0a64254718488d3c8012f533e.tar.gz pardes-553a62bac675bfc0a64254718488d3c8012f533e.zip | |
cli: a core made here is let go only after its serve thread has ended (the reorganisation's runners, step 10c, its third part: the crash once in 14 shell-test runs, a serve thread reading a freed core): the coordinator's trace (runtime/main.zig `Here.serve` -> Pardes.serveFs, reading `p.fs.edit_hold`) is the command line's own server, not the 9P listener: Tree.open served a core made in this process on a DETACHED thread, and the `cli:` test freed that core with its arena as soon as its read returned, while the thread was still answering the clunk the client sends and never waits for (cloud9 Fsys.clunk); the thread is kept now and Tree.close closes the client's end and joins it before the core's memory may go; cloud9's candidate (acceptLoop adding a connection's task while stop cancels the group) is not a fault: std.Io.Threaded's groupCancel waits until the group's running count is zero, the adder is itself a running task of the group so the count cannot reach zero between, and a task started after the cancel starts canceled (Group.Task.start); a test, `cli: a core made here is let go only once its serve thread has ended`, reads, closes and frees a core made here 300 times with the page allocator's arena (a read after is a fault): before the fix it segfaulted on its first run (Segmentation fault in the test process; the test runner still reported the step passed, so the gate never saw it); after it passes; shell-test 1017/1020 and gui unit-test 1615/1619 with it, the rest as change 1's (the same run); the frame and perf gate against tree1 (run5-1, on the tree with both changes): PASS, frames 77/77 byte-identical, corpus2x -0.151% (bar 0.4%), the 97 per-operation counts within 0.1% (largest +0.031%)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'runtime/main.zig')
| -rw-r--r-- | runtime/main.zig | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/runtime/main.zig b/runtime/main.zig index 92821c8a..2b600d63 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -595,6 +595,9 @@ const Here = struct { core: *pardes.Pardes, engine: Engine, fd: c_int, + /// Its serve thread, which reads the core until the other end hangs up + /// (`Tree.close` joins it). + thread: std.Thread, in: [@import("runtime").ninep.msize]u8, out: [2 * @import("runtime").ninep.msize]u8, buf: [@import("runtime").ninep.msize]u8, @@ -649,12 +652,23 @@ const Tree = struct { var fds: [2]std.c.fd_t = undefined; if (socketpair(std.c.AF.UNIX, std.c.SOCK.STREAM | std.c.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocketPair; h.fd = fds[1]; - (try std.Thread.spawn(.{}, Here.serve, .{h})).detach(); + h.thread = try std.Thread.spawn(.{}, Here.serve, .{h}); t.here = h; try t.f.init(io, .{ .socket = .{ .handle = fds[0], .address = undefined } }, "pardes"); return t; } + /// Let go (a test's; a command's process just ends). A core made here + /// is served to the end of what was sent it (a clunk, which the client + /// never waits for: Fsys.clunk) before its memory may go: its thread + /// sees the hangup and is joined. + fn close(t: *Tree) void { + t.f.close(); + const h = t.here orelse return; + h.thread.join(); + _ = std.c.close(h.fd); + } + /// The whole file at `path`. fn read(t: *Tree, gpa: std.mem.Allocator, path: []const u8) ![]const u8 { var out: std.Io.Writer.Allocating = .init(gpa); @@ -1861,7 +1875,7 @@ test "cli: docs/typ/command-line.txt is what pardes help --all prints" { defer arena.deinit(); const a = arena.allocator(); const t = try Tree.open(std.testing.io, a, null); - defer t.f.close(); + defer t.close(); var out: std.Io.Writer.Allocating = .init(a); try writeHelpAll(&out.writer, t); var dir = try std.Io.Dir.cwd().openDir(std.testing.io, @import("doc_typ").dir, .{}); @@ -1873,6 +1887,23 @@ test "cli: docs/typ/command-line.txt is what pardes help --all prints" { } } +test "cli: a core made here is let go only once its serve thread has ended: read, closed and freed at once, again and again" { + if (comptime !ninep_io.supported) return error.SkipZigTest; + // Each read ends with a clunk the client never waits for (Fsys.clunk): + // the serve thread is still answering it as the tree is closed and the + // core's memory let go (the page allocator's: unmapped, so a read of + // it after is a fault, not a stale value). + for (0..300) |_| { + var arena: std.heap.ArenaAllocator = .init(std.heap.page_allocator); + defer arena.deinit(); + const t = try Tree.open(std.testing.io, arena.allocator(), null); + defer t.close(); + var out: std.Io.Writer.Allocating = .init(arena.allocator()); + try t.f.readFile("README", &out.writer); + try std.testing.expect(out.written().len > 0); + } +} + test "cli: the first screen names every command, and every flag has its help" { var buf: [8192]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); |
