-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Open
Labels
C-bugCategory: This is a bug.Category: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.This issue may need triage. Remove it if it has been sufficiently triaged.
Description
File descriptors passed to Command::stdout/err/in are not closed after dup2 to replace stdout/stderr/stdin. This means the file descriptors passed to Command::stdout/err/in are leaked in the child process.
Example:
use std::fs::File;
use std::process::{Command, Stdio};
fn main() -> std::io::Result<()> {
let file = File::create("output.txt")?;
let mut child = Command::new("echo")
.arg("Hello from the child process!")
.stdout(Stdio::from(file))
.spawn()?;
let status = child.wait()?;
println!("Child exited with status: {}", status);
Ok(())
}
strace -f
output (full output is attached):
3903339 dup2(3, 1) = 1
3903339 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
3903339 execve("/home/i509vcb/.cargo/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/home/i509vcb/.local/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/home/i509vcb/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/usr/lib64/ccache/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/usr/local/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */) = -1 ENOENT (No such file or directory)
3903339 execve("/usr/bin/echo", ["echo", "Hello from the child process!"], 0x7fffc66d0a58 /* 75 vars */ <unfinished ...>
3903338 <... clone3 resumed>) = 3903339
This will leak fd 3 on the child process. Ideally do_exec
would close the stdio file descriptors which were overriden after dup2 on the child:
rust/library/std/src/sys/process/unix/unix.rs
Line 282 in 2300c2a
) -> Result<!, io::Error> { |
Metadata
Metadata
Assignees
Labels
C-bugCategory: This is a bug.Category: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.This issue may need triage. Remove it if it has been sufficiently triaged.