From 90b2661ad035c827b6f4b2e0f66ba344c4215b24 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 19 Jun 2024 21:00:37 -0700 Subject: [PATCH] fix: Exit without flushing stdout and stderr Standard out is flushed during `std::rt::cleanup()`, called by `std::process::exit()`, and standard error is unbuffered, so doesn't need to be flushed. --- clap_builder/src/error/mod.rs | 4 ++-- clap_builder/src/util/mod.rs | 9 --------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/clap_builder/src/error/mod.rs b/clap_builder/src/error/mod.rs index eacf2f3c..1e8fcd57 100644 --- a/clap_builder/src/error/mod.rs +++ b/clap_builder/src/error/mod.rs @@ -23,7 +23,7 @@ use crate::output::fmt::Colorizer; use crate::output::fmt::Stream; use crate::parser::features::suggestions; use crate::util::FlatMap; -use crate::util::{color::ColorChoice, safe_exit, SUCCESS_CODE, USAGE_CODE}; +use crate::util::{color::ColorChoice, SUCCESS_CODE, USAGE_CODE}; use crate::Command; #[cfg(feature = "error-context")] @@ -233,7 +233,7 @@ impl Error { pub fn exit(&self) -> ! { // Swallow broken pipe errors let _ = self.print(); - safe_exit(self.exit_code()) + std::process::exit(self.exit_code()); } /// Prints formatted and colored error to `stdout` or `stderr` according to its error kind diff --git a/clap_builder/src/util/mod.rs b/clap_builder/src/util/mod.rs index a92aef8c..8008db5a 100644 --- a/clap_builder/src/util/mod.rs +++ b/clap_builder/src/util/mod.rs @@ -29,15 +29,6 @@ pub(crate) const SUCCESS_CODE: i32 = 0; // - Python's `argparse` returns 2 pub(crate) const USAGE_CODE: i32 = 2; -pub(crate) fn safe_exit(code: i32) -> ! { - use std::io::Write; - - let _ = std::io::stdout().lock().flush(); - let _ = std::io::stderr().lock().flush(); - - std::process::exit(code) -} - #[cfg(not(feature = "unicode"))] pub(crate) fn eq_ignore_case(left: &str, right: &str) -> bool { left.eq_ignore_ascii_case(right)