From 722b576eb3a81a70ec2446bbb2d27c72e574af35 Mon Sep 17 00:00:00 2001 From: soulfy Date: Fri, 9 Aug 2024 14:20:24 +0800 Subject: [PATCH] agent: kill child process when console socket closed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit when use debug console, the shell run in child process may not be exited, in some scenes. eg. directly Ctrl-C in the host to terminate the kata-runtime process, that will block the task handling the console connection,while waiting for the child to exit. Signed-off-by: soulfy --- src/agent/src/console.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/agent/src/console.rs b/src/agent/src/console.rs index 8f1ae5ff32..49a9226e8b 100644 --- a/src/agent/src/console.rs +++ b/src/agent/src/console.rs @@ -11,7 +11,7 @@ use nix::libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; use nix::pty::{openpty, OpenptyResult}; use nix::sys::socket::{self, AddressFamily, SockFlag, SockType, VsockAddr}; use nix::sys::stat::Mode; -use nix::sys::wait; +use nix::sys::{signal, wait}; use nix::unistd::{self, close, dup2, fork, setsid, ForkResult, Pid}; use rustjail::pipestream::PipeStream; use slog::Logger; @@ -178,6 +178,13 @@ async fn run_in_parent( ); } res = tokio::io::copy(&mut socket_reader, &mut master_writer) => { + // the shell run in child may not be exited, in some scenes + // eg. directly Ctrl-C in the host to terminate the kata-runtime process + // that will block this task,while waiting for the child to exit. + // + let _ = signal::kill(child_pid, Some(signal::Signal::SIGKILL)) + .map_err(|e| warn!(logger, "kill child shell process {:?}", e)); + info!( logger, "socket closed: {:?}", res