From b644390014fc174aa5bc768c83ff6bbf7905f34d Mon Sep 17 00:00:00 2001 From: Eric Zhang Date: Tue, 14 Nov 2023 19:56:53 -0500 Subject: [PATCH] Call waitpid to reap defunct terminal processes --- crates/sshx/src/terminal.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/sshx/src/terminal.rs b/crates/sshx/src/terminal.rs index 341b4d0..8b9933d 100644 --- a/crates/sshx/src/terminal.rs +++ b/crates/sshx/src/terminal.rs @@ -15,6 +15,7 @@ use nix::errno::Errno; use nix::libc::{login_tty, TIOCGWINSZ, TIOCSWINSZ}; use nix::pty::{self, Winsize}; use nix::sys::signal::{kill, Signal::SIGKILL}; +use nix::sys::wait::waitpid; use nix::unistd::{execvp, fork, ForkResult, Pid}; use pin_project::{pin_project, pinned_drop}; use tokio::fs::File; @@ -146,10 +147,16 @@ impl AsyncWrite for Terminal { impl PinnedDrop for Terminal { fn drop(self: Pin<&mut Self>) { let this = self.project(); - trace!(child = %this.child, "dropping terminal"); + let child = *this.child; + trace!(%child, "dropping terminal"); - // Reap the child process on closure so that it doesn't keep running. - kill(*this.child, SIGKILL).ok(); + // Kill the child process on closure so that it doesn't keep running. + kill(child, SIGKILL).ok(); + + // Reap the zombie process in a background thread. + std::thread::spawn(move || { + waitpid(child, None).ok(); + }); } }