termio/exec: don't leak zombie subprocesses

This commit is contained in:
Will Pragnell
2023-08-30 21:34:34 -07:00
parent 3352cae3f7
commit aa9e12dac2
2 changed files with 11 additions and 3 deletions

View File

@ -190,7 +190,14 @@ pub fn wait(self: Command, block: bool) !Exit {
// We specify NOHANG because its not our fault if the process we launch
// for the tty doesn't properly waitpid its children. We don't want
// to hang the terminal over it.
const res = std.os.waitpid(self.pid.?, if (block) 0 else std.c.W.NOHANG);
// When NOHANG is specified, waitpid will return a pid of 0 if the process
// doesn't have a status to report. When that happens, it is as though the
// wait call has not been performed, so we need to keep trying until we get
// a non-zero pid back, otherwise we end up with zombie processes.
var res = std.os.WaitPidResult{ .pid = 0, .status = 0 };
while (res.pid == 0) {
res = std.os.waitpid(self.pid.?, if (block) 0 else std.c.W.NOHANG);
}
return Exit.init(res.status);
}

View File

@ -831,8 +831,9 @@ const Subprocess = struct {
}
/// Stop the subprocess. This is safe to call anytime. This will wait
/// for the subprocess to end so it will block. This does not close
/// the pty.
/// for the subprocess to register that it has been signalled, but not
/// for it to terminate, so it will not block.
/// This does not close the pty.
pub fn stop(self: *Subprocess) void {
// Kill our command
if (self.command) |*cmd| {