diff options
| -rw-r--r-- | dev/lsp/lsp_client.zig | 5 | ||||
| -rw-r--r-- | dev/tui/tui.zig | 4 | ||||
| -rw-r--r-- | test/lspmock.zig | 26 | ||||
| -rw-r--r-- | test/process/tty_editing.zig | 55 |
4 files changed, 86 insertions, 4 deletions
diff --git a/dev/lsp/lsp_client.zig b/dev/lsp/lsp_client.zig index 4ad8dace..fdd4af55 100644 --- a/dev/lsp/lsp_client.zig +++ b/dev/lsp/lsp_client.zig @@ -2244,8 +2244,9 @@ fn reap(pid: libc.pid_t) void { /// At this process's exit, every server's group is killed: a server busy /// in a build (zls at a cold start) would not notice its socket close, and /// lived on with its children after the editor. Registered once, at the -/// first spawn; signals only, no locks (other threads may hold them). -fn killAllAtExit() callconv(.c) void { +/// first spawn; signals only, no locks (other threads may hold them): a +/// signal's handler calls it too (tui.zig `Killed`). +pub fn killAllAtExit() callconv(.c) void { for (&conns) |*c| if (c.pid > 0) { _ = libc.kill(-c.pid, .KILL); }; diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 72028b04..638f1400 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -906,7 +906,7 @@ const Screen = struct { /// the main screen, no mouse, paste or keyboard modes, the cursor shown, /// cooked termios -- and then the signal's own death. A nested editor /// killed in a pane must not leave that pane's terminal wedged. -/// Async-signal-safe: write, tcsetattr, sigaction, raise. +/// Async-signal-safe: write, tcsetattr, kill, sigaction, raise. const Killed = struct { var fd: posix.fd_t = -1; var cooked: posix.termios = undefined; @@ -937,6 +937,8 @@ const Killed = struct { ninep_io.unlinkOwnSocket(); host_io.Shell.unlinkOwnedRcs(); daemon.hangUp(); + // No exit hook runs: the language servers' groups go here. + @import("../lsp/lsp_client.zig").killAllAtExit(); // SA_RESETHAND put the default back: the same signal, now fatal. _ = std.c.raise(sig); } diff --git a/test/lspmock.zig b/test/lspmock.zig index 001c3488..8b5c73a8 100644 --- a/test/lspmock.zig +++ b/test/lspmock.zig @@ -15,12 +15,17 @@ //! mute handshake, then never answer another request (timeout path) //! die exit right after the handshake (dead-server path) //! utf16 refuse the utf-8 negotiation (column-conversion path) +//! busy at the handshake, leave a child running in the server's +//! group, as a build a server started does; its pid said in +//! `lspmock-busy.pid` in the server's directory (its root) const std = @import("std"); const libc = std.c; extern "c" fn getenv(name: [*:0]const u8) ?[*:0]const u8; +extern "c" fn fork() libc.pid_t; +extern "c" fn pause() c_int; -var mode: enum { normal, mute, die, utf16 } = .normal; +var mode: enum { normal, mute, die, utf16, busy } = .normal; /// stdout, written with plain libc write(2): the transport is a socketpair /// and the frames are small, so partial writes are retried and nothing else @@ -118,6 +123,7 @@ pub fn main(init: std.process.Init) !void { if (std.mem.eql(u8, s, "mute")) mode = .mute; if (std.mem.eql(u8, s, "die")) mode = .die; if (std.mem.eql(u8, s, "utf16")) mode = .utf16; + if (std.mem.eql(u8, s, "busy")) mode = .busy; } const gpa = std.heap.page_allocator; @@ -164,6 +170,23 @@ pub fn main(init: std.process.Init) !void { } } +/// A child that runs on in this server's group, its socket closed, and +/// says its pid. +fn leaveChild() void { + const pid = fork(); + if (pid != 0) return; + _ = libc.close(0); + _ = libc.close(1); + var buf: [16]u8 = undefined; + const said = std.fmt.bufPrint(&buf, "{d}\n", .{libc.getpid()}) catch unreachable; + const fd = libc.open("lspmock-busy.pid", .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o600)); + if (fd >= 0) { + _ = libc.write(fd, said.ptr, said.len); + _ = libc.close(fd); + } + while (true) _ = pause(); +} + fn handle(a: std.mem.Allocator, raw: []const u8) void { const v = std.json.parseFromSliceLeaky(std.json.Value, a, raw, .{}) catch return; const method = str(get(v, "method")) orelse ""; @@ -195,6 +218,7 @@ fn handle(a: std.mem.Allocator, raw: []const u8) void { } if (std.mem.eql(u8, method, "initialized")) { if (mode == .die) std.process.exit(0); + if (mode == .busy) leaveChild(); // the progress trio, so the status pipeline has something to narrate notify(a, "$/progress", "{{\"token\":\"t1\",\"value\":{{\"kind\":\"begin\",\"title\":\"mocking\"}}}}", .{}); notify(a, "$/progress", "{{\"token\":\"t1\",\"value\":{{\"kind\":\"report\",\"percentage\":50,\"message\":\"halfway\"}}}}", .{}); diff --git a/test/process/tty_editing.zig b/test/process/tty_editing.zig index 63c75512..53e1da1e 100644 --- a/test/process/tty_editing.zig +++ b/test/process/tty_editing.zig @@ -27,6 +27,7 @@ pub const cases = .{ .{ "tty editing: lsp", lsp }, .{ "tty editing: lsp-client", lsp_client }, .{ "tty editing: lsp-client-edit", lsp_client_edit }, + .{ "tty editing: lsp-hangup", lsp_hangup }, .{ "tty editing: lspcomplete", lspcomplete }, .{ "tty editing: lspcompletemoved", lspcompletemoved }, .{ "tty editing: lspdebug", lspdebug }, @@ -1511,6 +1512,60 @@ fn lsp_client(gpa: std.mem.Allocator, exe: [:0]const u8) !void { try t.done(); } +// A pardes with no daemon whose terminal hangs up dies by the signal, and +// no exit hook runs: its language servers' groups go with it all the same. +// The mock, busy (LSPMOCK_MODE=busy), leaves a child in its group, as zls +// in a build does, which its socket's end does not stop. +fn lsp_hangup(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var t = try Tty.init(gpa, exe, "lsp-hangup"); + defer t.deinit(); + _ = setenv("LSPMOCK_MODE", "busy", 1); + // The server's root, where it says its child's pid. + try t.file("Cargo.toml", ""); + try t.file("main.rs", "fn main() {}\n"); + try t.start(25, 100, &.{"main.rs"}); + try t.wait(8000, "fn main()"); + // A request starts the server (goto-definition). + try t.key("g"); + try t.key("d"); + var buf: [32]u8 = undefined; + const child: std.c.pid_t = for (0..1000) |_| { + const fd = std.c.open("lspmock-busy.pid", .{}); + if (fd >= 0) { + const n = std.c.read(fd, &buf, buf.len); + _ = std.c.close(fd); + if (n > 0) if (std.fmt.parseInt(std.c.pid_t, std.mem.trim(u8, buf[0..@intCast(n)], " \n"), 10)) |pid| break pid else |_| {}; + } + try t.settle(10); + } else return error.NoServerChild; + // Whatever the test's end, the child it made goes. + defer _ = std.c.kill(child, .KILL); + t.h.?.deinit(); // the terminal hangs up + t.h = null; + for (0..500) |_| { + if (!alive(child)) return; + var none: [0]std.posix.pollfd = .{}; + _ = std.posix.poll(&none, 10) catch 0; + } + return error.ServerChildLivesOn; +} + +/// Whether `pid` runs (a zombie waiting for its reaper does not). +fn alive(pid: std.c.pid_t) bool { + var path: [32]u8 = undefined; + const p = std.fmt.bufPrintSentinel(&path, "/proc/{d}/stat", .{pid}, 0) catch return false; + const fd = std.c.open(p, .{}); + if (fd < 0) return false; + defer _ = std.c.close(fd); + var stat: [256]u8 = undefined; + const n = std.c.read(fd, &stat, stat.len); + if (n <= 0) return false; + const close_paren = std.mem.lastIndexOfScalar(u8, stat[0..@intCast(n)], ')') orelse return false; + return close_paren + 2 < n and stat[close_paren + 2] != 'Z'; +} + +extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; + // The protocol client's MUTATING answers, through the @put record path: a // server TextEdit response becomes one atomic buffer edit with one undo step. // = applies the mock's formatting edit (a header line) and narrates |
