From 3eb6c09bc97c2e60a17e1663108f4b22e3d1308c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 01:45:46 -0300 Subject: dev, runtime: a session's file watches once (file_watch.Watches, a working name), the session loop's beside its ptys and queue: the kernel's descriptor, the table (each pane's mark and the theme's slot), the reader that posts `files_changed` to the one queue (the Inbox) and the Host's three file calls (Watches.Calls fills both frontends' write_file, watch_file, watch_theme); the terminal's reader (an Io task posting to vaxis's queue, its files_changed message) and the window's (a thread with a stop pipe, polling a non-blocking descriptor) are deleted for one Io task on a blocking descriptor ended by its cancel, as the ptys' readers are; a Restore lets go of the loop's table, so the frontend call `watched` is gone (ten calls to nine); what a user sees: nothing (the queue keeps one change flag however many reads posted it; a reader that cannot start leaves the session running unwatched, as the terminal's did, where the window's start failed); tui core-test 857/1007 (150 skipped; +1, the device's test), shell-test 1047/1050, term 17/17, pane 183/184, kitty 45/45, behaviour-test and --replay 275/275, process-test 0 of 144 failed (ttytaken once in the run, passed on its retry), soaks --clients 5 and --bytes --clients 5 diverged 0, unequal 0, quiet checks 597/0 673/0 779/0 and 920/0 723/0 755/0, unlisted 0, corpus scene 22 of 22, no leak; gui unit-test 1643/1647 with only the perf check's BaselineHarnessMismatch; daemon.py plain, --far, --games, --window-clients, --upgrade both ways pass; the macOS library (Linux, aarch64-macos) and FreeBSD build (load 9-18) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- dev/gui/gui.zig | 121 ++++++-------------------------------------------------- 1 file changed, 12 insertions(+), 109 deletions(-) (limited to 'dev/gui/gui.zig') diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index c3272957..59e0516a 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -382,32 +382,6 @@ fn sdlWake(_: ?*anyopaque) void { const PipeTasks = selection_pipe.Tasks; const output_capacity = Queue.output_capacity; -fn stopPipe() ![2]c_int { - var fds: [2]c_int = undefined; - if (libc.pipe(&fds) != 0) return error.PipeFailed; - errdefer for (fds) |fd| { - _ = libc.close(fd); - }; - for (fds) |fd| if (libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)) < 0) return error.PipeFailed; - return fds; -} - -fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { - while (true) { - var fds = [_]libc.pollfd{ - .{ .fd = stop, .events = libc.POLL.IN, .revents = 0 }, - .{ .fd = fd, .events = libc.POLL.IN, .revents = 0 }, - }; - if (libc.poll(&fds, fds.len, -1) < 0) { - if (libc.errno(-1) == .INTR) continue; - return; - } - if (fds[0].revents != 0) return; - if ((fds[1].revents & (libc.POLL.ERR | libc.POLL.HUP | libc.POLL.NVAL)) != 0) return; - if (fds[1].revents != 0 and file_watch.drain(fd)) q.push(.files_changed); - } -} - const Gui = struct { addon_gpu: ?SketchGpu = null, addon_hash: [pardes.MAX_PANES]u64 = @splat(0), @@ -1017,10 +991,6 @@ const Own = struct { test_mode: bool, detached: bool, pipe_tasks: PipeTasks = .{}, - inotify_fd: c_int = -1, - watch_reader: ?std.Thread = null, - watch_stop: [2]c_int = .{ -1, -1 }, - watches: file_watch.Table = @splat(null), shell: Shell = undefined, const Session = session_loop.Loop(Own); @@ -1045,7 +1015,6 @@ const Own = struct { observeGuiFont(g, core); syncTaglineFont(g, core); l.inbox.wake_fn = sdlWake; - o.inotify_fd = file_watch.init(true); o.shell = .{ .core = core, .gui = g, @@ -1055,8 +1024,7 @@ const Own = struct { .ptys = &l.ptys, .queue = &l.inbox, .pipe_tasks = &o.pipe_tasks, - .inotify_fd = o.inotify_fd, - .watches = &o.watches, + .watches = &l.watches, .fs = l.fs, .test_mode = o.test_mode, .test_clock = if (o.test_mode) host_io.testClock() else null, @@ -1082,18 +1050,6 @@ const Own = struct { pardes.lsp.setStatusSink(null, null); pardes.Kind.drawer = null; o.shell.job_tasks.cancelAll(io); - if (o.inotify_fd >= 0) { - if (o.watch_reader) |thread| { - _ = host_io.writeFd(o.watch_stop[1], "x"); - thread.join(); - } - for (o.watch_stop) |fd| if (fd >= 0) { - _ = libc.close(fd); - }; - file_watch.stop(o.inotify_fd); - _ = libc.close(o.inotify_fd); - o.inotify_fd = -1; - } o.pipe_tasks.cancelAll(io); if (l.inbox.lsp_task) |*task| task.future.cancel(io); // Its worker may be waiting on the queue for room. @@ -1102,10 +1058,6 @@ const Own = struct { } pub fn start(o: *Own, l: *Session) !void { - if (o.inotify_fd >= 0) { - o.watch_stop = try stopPipe(); - o.watch_reader = try std.Thread.spawn(.{}, watchThread, .{ o.inotify_fd, o.watch_stop[0], &l.inbox }); - } if (l.fs) |f| try f.watch(&l.inbox, wakeFs); _ = c.SDL_StartTextInput(o.g.window); @@ -1133,10 +1085,6 @@ const Own = struct { deliverNote(&o.shell, n); } - pub fn watched(o: *Own) session_loop.Watched { - return .{ .fd = o.inotify_fd, .table = &o.watches }; - } - pub fn restoring(o: *Own, nc: *pardes.Pardes) void { o.pipe_tasks.cancelAll(o.shell.io); o.shell.queue.discardCompletions(); @@ -1471,7 +1419,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ defer queue.deinit(); var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = undefined }; var pipe_tasks: PipeTasks = .{}; - var watches: file_watch.Table = @splat(null); + var no_watches: file_watch.Watches = .{ .io = io, .gpa = gpa, .queue = &queue }; var no_peers: replica.Peers = .{}; var shell: Shell = .{ .core = start.r.core(), @@ -1482,8 +1430,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ .ptys = &ptys, .queue = &queue, .pipe_tasks = &pipe_tasks, - .inotify_fd = -1, - .watches = &watches, + .watches = &no_watches, .test_mode = test_mode, .client = &window, .peers = &no_peers, @@ -2411,8 +2358,9 @@ const Shell = struct { batch: Queue.Batch = .{}, pipe_tasks: *PipeTasks, job_tasks: PipeTasks = .{}, - inotify_fd: c_int, - watches: *file_watch.Table, + /// The session's file watches (runtime/session.zig's); a client's, + /// none. + watches: *file_watch.Watches, threads_ok: bool = false, test_mode: bool = false, feed: StdinFeed = .{}, @@ -2605,10 +2553,10 @@ const Shell = struct { .tty_taken = PtyCalls.ttyTaken, .fg_name = PtyCalls.fgName, .kill_job = PtyCalls.killJob, - .write_file = writeFile, + .write_file = WatchCalls.writeFile, .write_dump = writeDump, - .watch_file = watchFile, - .watch_theme = watchTheme, + .watch_file = WatchCalls.watchFile, + .watch_theme = WatchCalls.watchTheme, .dump_themes = dumpThemes, .set_clipboard = setClipboard, .read_clipboard = readClipboard, @@ -2622,6 +2570,7 @@ const Shell = struct { }; const PtyCalls = host_io.Ptys.Calls(Shell); + const WatchCalls = file_watch.Watches.Calls(Shell); /// The command panes' exits, told once their output is in (host_io). fn takeExits(s: *Shell) void { @@ -2689,8 +2638,7 @@ const Shell = struct { .window => if (s.gui == null) s.openTheWindow(), .adopted => |a| s.ptys.adoptions.adoptShell(s.core, a.pid, a.fd), }; - if (check_files and file_watch.reloadChanged(s.core, s.io, s.gpa, s.watches)) - s.queue.push(.files_changed); + if (check_files) s.watches.reload(s.core); } }; @@ -3216,35 +3164,10 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { } } -fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void { - const pane = id.index(); - const s = shellOf(ctx); - host_io.writeFile(s.core, s.watches, pane, path, bytes); -} - fn writeDump(ctx: ?*anyopaque, bytes: []const u8) void { host_io.writeDump(shellOf(ctx).core, bytes); } -fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, on: bool, mode: pardes.WatchMode) void { - const pane = id.index(); - const s = shellOf(ctx); - if (file_watch.applyEffect(s.core, s.io, s.inotify_fd, s.watches, pane, on, mode)) - s.queue.push(.files_changed); -} - -fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void { - const s = shellOf(ctx); - if (file_watch.applyThemeEffect( - s.core, - s.gpa, - s.inotify_fd, - s.watches, - generation, - on, - )) s.queue.push(.files_changed); -} - fn dumpThemes(ctx: ?*anyopaque, id: pardes.Pane.Id) void { const pane = id.index(); const s = shellOf(ctx); @@ -6041,25 +5964,6 @@ test "installed Nerd Symbols fallback covers Yazi directory icons" { try std.testing.expect(atlas.glyphForCodepoint(0xe5ff).glyph != 0); } -test "GUI PTY file watcher stops and joins without closing its watched descriptor" { - if (!file_watch.supported) return error.SkipZigTest; - const gpa = std.testing.allocator; - var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; - defer queue.deinit(); - const fd = file_watch.init(true); - if (fd < 0) return error.WatchInitFailed; - defer _ = libc.close(fd); - const stop = try stopPipe(); - defer for (stop) |pipe_fd| { - _ = libc.close(pipe_fd); - }; - const thread = try std.Thread.spawn(.{}, watchThread, .{ fd, stop[0], &queue }); - _ = host_io.writeFd(stop[1], "x"); - thread.join(); - try std.testing.expect(libc.fcntl(fd, libc.F.GETFD) >= 0); - try std.testing.expectEqual(@as(usize, 0), queue.take().len); -} - test "GUI completion failure survives backlog and leaves the document unchanged" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); @@ -7809,9 +7713,8 @@ test "a detached GUI authority follows terminal directory changes with no window var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = &rcs, .gens = @splat(1) }; defer ptys.deinit(); var pipes: PipeTasks = .{}; - var watches: file_watch.Table = @splat(null); var no_peers: replica.Peers = .{}; - var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .ptys = &ptys, .queue = &queue, .pipe_tasks = &pipes, .inotify_fd = -1, .watches = &watches, .peers = &no_peers }; + var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .ptys = &ptys, .queue = &queue, .pipe_tasks = &pipes, .watches = undefined, .peers = &no_peers }; const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); ptys.slots[0] = .{ .fd = child.file.handle, .pid = child.pid }; try std.testing.expect(host_io.writeFd(child.file.handle, "cd child\n")); -- cgit v1.3