termio: killpg expected to fail on darwin, still go into waitpid loop

Fixes #2273

On macOS, killpg is expected to fail with EPERM because of the way we
launch a login process around it. Before this commit, this caused us to
never call waitpid and reap the child process, which caused the child
process to stick around as a zombie.

This commit allows killpg to fail with EPERM on macOS and fall through
to waitpid.
This commit is contained in:
Mitchell Hashimoto
2024-09-20 15:21:37 -07:00
parent f5c83d62d7
commit f8bdd2b1bb

View File

@ -1257,9 +1257,19 @@ const Subprocess = struct {
// descendents are well and truly dead. We will not rest
// until the entire family tree is obliterated.
while (true) {
if (c.killpg(pgid, c.SIGHUP) < 0) {
log.warn("error killing process group pgid={}", .{pgid});
return error.KillFailed;
switch (posix.errno(c.killpg(pgid, c.SIGHUP))) {
.SUCCESS => log.debug("process group killed pgid={}", .{pgid}),
else => |err| killpg: {
if ((comptime builtin.target.isDarwin()) and
err == .PERM)
{
log.debug("killpg failed with EPERM, expected on Darwin and ignoring", .{});
break :killpg;
}
log.warn("error killing process group pgid={} err={}", .{ pgid, err });
return error.KillFailed;
},
}
// See Command.zig wait for why we specify WNOHANG.
@ -1267,6 +1277,7 @@ const Subprocess = struct {
// are still alive without blocking so that we can
// kill them again.
const res = posix.waitpid(pid, std.c.W.NOHANG);
log.debug("waitpid result={}", .{res.pid});
if (res.pid != 0) break;
std.time.sleep(10 * std.time.ns_per_ms);
}