From e9e074c1e70f7a6c941deca31116882c434fd3b7 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 8 Nov 2026 20:50:30 -0300 Subject: fix: a window handover under a runtime directory too long for a window's 9P socket made the window and waited 15 s on a registry entry that could never answer (past ~490 bytes its path was empty): it says so first, no new window: XDG_RUNTIME_DIR is N bytes, too long for its 9P socket; daemon.find's reason for a name past its room is cut, never empty (Zig 0.17's bufPrint writes what fits), and a test pins that Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Handover.zig | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'src/Handover.zig') diff --git a/src/Handover.zig b/src/Handover.zig index 0abcf859..1b4fed7d 100644 --- a/src/Handover.zig +++ b/src/Handover.zig @@ -402,12 +402,17 @@ fn carry(io: std.Io, job: *Job) void { name, if (err == error.Remote) ninep_io.Client.lastSaid() else @errorName(err), }) catch "the window would not take it"); } + // A runtime directory a window's socket cannot be made in (9p_io's + // `/pardes-9p-.sock.`, a pid at most 7 digits) is said + // before a window is made that could never answer. + const run = std.mem.span(libc.getenv("XDG_RUNTIME_DIR") orelse ""); + if (run.len + "/pardes-9p-.sock.".len + 2 * "4194304".len + 1 > ninep_io.sun_path_len) + return job.post(job.post_ctx, job.seq, false, std.fmt.bufPrint(&words, "no new window: XDG_RUNTIME_DIR is {d} bytes, too long for its 9P socket", .{run.len}) catch unreachable); // a number fits 256 const pid = spawnWindow(job) catch |err| return job.post(job.post_ctx, job.seq, false, std.fmt.bufPrint(&words, "no new window: {s}", .{@errorName(err)}) catch "no new window"); // Up when its registry entry answers: it read the dump before it // listened (gui.localSession), so the file is no longer needed. var entry_buf: [512]u8 = undefined; - const run = libc.getenv("XDG_RUNTIME_DIR") orelse ""; - const entry = std.fmt.bufPrint(&entry_buf, "{s}/9p/pardes/{d}", .{ std.mem.span(run), pid }) catch ""; + const entry = std.fmt.bufPrint(&entry_buf, "{s}/9p/pardes/{d}", .{ run, pid }) catch unreachable; // under sun_path, checked above var waited: u32 = 0; while (waited < 15_000) : (waited += 50) { if (libc.kill(pid, @enumFromInt(0)) != 0) @@ -652,3 +657,41 @@ test "a column let go over another window goes beside the column the pointer is from.update(.{ .handed = .{ .seq = from.handover.seq, .to = "pardes/2" } }); try std.testing.expect(from.view().ncol == 1 or from.view().cols[c].n == 0 or layout.findPane(from, 0) == null); } + +extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; + +test "a handover under a runtime directory too long for a window's socket is refused in words, no window made" { + if (comptime builtin.os.tag != .linux) return error.SkipZigTest; + const gpa = std.testing.allocator; + const Said = struct { + var ok: ?bool = null; + var buf: [256]u8 = undefined; + var len: usize = 0; + fn post(_: ?*anyopaque, _: u32, ok_: bool, words: []const u8) void { + ok = ok_; + len = @min(words.len, buf.len); + @memcpy(buf[0..len], words[0..len]); + } + }; + const old = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; + defer if (old) |v| { + _ = setenv("XDG_RUNTIME_DIR", v, 1); + gpa.free(v); + } else { + _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); + }; + // The ledger's case, past 490 bytes, and one past sun_path alone. + for ([_]usize{ 600, 100 }) |n| { + var dir_buf: [601:0]u8 = undefined; + @memset(dir_buf[0..n], 'r'); + dir_buf[0] = '/'; + dir_buf[n] = 0; + try std.testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", dir_buf[0..n :0], 1)); + const job = try gpa.create(Job); + job.* = .{ .gpa = gpa, .seq = 1, .path = try gpa.dupeSentinel(u8, "/nonexistent/pardes-handover.xitdb", 0), .post = Said.post, .post_ctx = null }; + Said.ok = null; + carry(std.testing.io, job); + try std.testing.expectEqual(@as(?bool, false), Said.ok); + try std.testing.expect(std.mem.indexOf(u8, Said.buf[0..Said.len], "XDG_RUNTIME_DIR is") != null); + } +} -- cgit v1.3