diff options
| -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); |
