From 83f54eec529ee72af822997ea3714f5abbc7c127 Mon Sep 17 00:00:00 2001 From: Fupan Li Date: Wed, 2 Jul 2025 17:25:45 +0800 Subject: [PATCH] agent: fix the issue of parent writer pipe fd leak Sometimes, containers or execs do not use stdin, so there is no chance to add parent stdin to the process's writer hashmap, resulting in the parent stdin's fd not being closed when the process is cleaned up later. Therefore, when creating a process, first explicitly add parent stdin to the wirter hashmap. Make sure that the parent stdin's fd can be closed when the process is cleaned up later. Signed-off-by: Fupan Li --- src/agent/rustjail/src/container.rs | 4 ++-- src/agent/rustjail/src/process.rs | 9 +++++++-- src/agent/src/rpc.rs | 5 ----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/agent/rustjail/src/container.rs b/src/agent/rustjail/src/container.rs index a35c5b62dd..bc4dd09073 100644 --- a/src/agent/rustjail/src/container.rs +++ b/src/agent/rustjail/src/container.rs @@ -2081,8 +2081,8 @@ mod tests { }); } - #[test] - fn test_linuxcontainer_get_process() { + #[tokio::test] + async fn test_linuxcontainer_get_process() { let _ = new_linux_container_and_then(|mut c: LinuxContainer| { c.processes.insert( 1, diff --git a/src/agent/rustjail/src/process.rs b/src/agent/rustjail/src/process.rs index d912f86954..3f41b1e1b9 100644 --- a/src/agent/rustjail/src/process.rs +++ b/src/agent/rustjail/src/process.rs @@ -179,6 +179,11 @@ impl Process { p.parent_stdin = Some(pstdin); p.stdin = Some(stdin); + // Make sure the parent stdin writer be inserted into + // p.writes hashmap, thus the cleanup_process_stream can + // cleanup and close the parent stdin fd. + let _ = p.get_writer(StreamType::ParentStdin); + // These pipes are necessary as the stdout/stderr of the child process // cannot be a socket. Otherwise, some images relying on the /dev/stdout(stderr) // and /proc/self/fd/1(2) will fail to boot as opening an existing socket @@ -308,8 +313,8 @@ mod tests { assert_eq!(max_size, actual_size); } - #[test] - fn test_process() { + #[tokio::test] + async fn test_process() { let id = "abc123rgb"; let init = true; let process = Process::new( diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index dfb762ddcd..ba41b3da4e 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -2623,11 +2623,6 @@ mod tests { }), ..Default::default() }, - TestData { - has_fd: false, - result: Err(anyhow!(ERR_CANNOT_GET_WRITER)), - ..Default::default() - }, ]; for (i, d) in tests.iter().enumerate() {