diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 0ffa2c5b8..277b582eb 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -796,17 +796,21 @@ impl Document { command: fmt_cmd.to_string_lossy().into(), error: e.kind(), })?; - { - let mut stdin = process.stdin.take().ok_or(FormatterError::BrokenStdin)?; - to_writer(&mut stdin, (encoding::UTF_8, false), &text) - .await - .map_err(|_| FormatterError::BrokenStdin)?; - } - let output = process - .wait_with_output() - .await - .map_err(|_| FormatterError::WaitForOutputFailed)?; + let mut stdin = process.stdin.take().ok_or(FormatterError::BrokenStdin)?; + let input_text = text.clone(); + let input_task = tokio::spawn(async move { + to_writer(&mut stdin, (encoding::UTF_8, false), &input_text).await + // Note that `stdin` is dropped here, causing the pipe to close. This can + // avoid a deadlock with `wait_with_output` below if the process is waiting on + // stdin to close before exiting. + }); + let (input_result, output_result) = tokio::join! { + input_task, + process.wait_with_output(), + }; + let _ = input_result.map_err(|_| FormatterError::BrokenStdin)?; + let output = output_result.map_err(|_| FormatterError::WaitForOutputFailed)?; if !output.status.success() { if !output.stderr.is_empty() {