From 695a9f3cb9355e64cde51cbd516ab6724ef2b1ce Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 6 Feb 2023 16:05:41 -0800 Subject: [PATCH] termio: fix pty close error so close doesn't hang on macOS --- src/termio/Exec.zig | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 1c4bc4b69..c6e382750 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -429,12 +429,6 @@ const Subprocess = struct { /// Stop the subprocess. This is safe to call anytime. This will wait /// for the subprocess to end so it will block. pub fn stop(self: *Subprocess) void { - // Close our PTY - if (self.pty) |*pty| { - pty.deinit(); - self.pty = null; - } - // Kill our command if (self.command) |*cmd| { killCommand(cmd) catch |err| @@ -443,6 +437,14 @@ const Subprocess = struct { log.err("error waiting for command to exit: {}", .{err}); self.command = null; } + + // Close our PTY. We do this after killing our command because on + // macOS, close will block until all blocking operations read/write + // are done with it and our reader thread is probably still alive. + if (self.pty) |*pty| { + pty.deinit(); + self.pty = null; + } } /// Resize the pty subprocess. This is safe to call anytime. @@ -519,7 +521,10 @@ const ReadThread = struct { // gracefully shutting down. error.NotOpenForReading => log.info("io reader exiting", .{}), - else => log.err("READ ERROR err={}", .{err}), + else => { + log.err("io reader error err={}", .{err}); + unreachable; + }, } return; };