From 1601de119647c7a0413b0f7058dabddc3aae3e66 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Mon, 10 Jan 2022 11:40:28 +0100 Subject: [PATCH] process: drop pipe after child exits in `wait_with_output` (#4315) --- tokio/src/process/mod.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tokio/src/process/mod.rs b/tokio/src/process/mod.rs index 8a0d9db25fd..67d43b4d80e 100644 --- a/tokio/src/process/mod.rs +++ b/tokio/src/process/mod.rs @@ -1130,19 +1130,26 @@ impl Child { pub async fn wait_with_output(mut self) -> io::Result { use crate::future::try_join3; - async fn read_to_end(io: Option) -> io::Result> { + async fn read_to_end(io: &mut Option) -> io::Result> { let mut vec = Vec::new(); - if let Some(mut io) = io { - crate::io::util::read_to_end(&mut io, &mut vec).await?; + if let Some(io) = io.as_mut() { + crate::io::util::read_to_end(io, &mut vec).await?; } Ok(vec) } - let stdout_fut = read_to_end(self.stdout.take()); - let stderr_fut = read_to_end(self.stderr.take()); + let mut stdout_pipe = self.stdout.take(); + let mut stderr_pipe = self.stderr.take(); + + let stdout_fut = read_to_end(&mut stdout_pipe); + let stderr_fut = read_to_end(&mut stderr_pipe); let (status, stdout, stderr) = try_join3(self.wait(), stdout_fut, stderr_fut).await?; + // Drop happens after `try_join` due to + drop(stdout_pipe); + drop(stderr_pipe); + Ok(Output { status, stdout,