From f1630a70f5375009c59cafb72db95cfbe00c2f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Ehlert?= Date: Mon, 8 Apr 2024 19:18:53 +0200 Subject: [PATCH] Suppress `MultipleHandlers` from Ctrl-C in confirm (#2903) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Fixes #2900 ## Test Plan Tried reproducing the steps described in #2900, but with `cargo run -- pip ...` and it didn't crash 😄. --- crates/uv-requirements/src/confirm.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/uv-requirements/src/confirm.rs b/crates/uv-requirements/src/confirm.rs index 3b111d331..7eb7d8b59 100644 --- a/crates/uv-requirements/src/confirm.rs +++ b/crates/uv-requirements/src/confirm.rs @@ -6,7 +6,8 @@ use console::{style, Key, Term}; /// This is a slimmed-down version of `dialoguer::Confirm`, with the post-confirmation report /// enabled. pub(crate) fn confirm(message: &str, term: &Term, default: bool) -> Result { - ctrlc::set_handler(move || { + // Set the Ctrl-C handler to exit the process. + let result = ctrlc::set_handler(move || { let term = Term::stderr(); term.show_cursor().ok(); term.flush().ok(); @@ -17,7 +18,16 @@ pub(crate) fn confirm(message: &str, term: &Term, default: bool) -> Result } else { 130 }); - })?; + }); + + match result { + Ok(()) => {} + Err(ctrlc::Error::MultipleHandlers) => { + // If multiple handlers were set, we assume that the existing handler is our + // confirmation handler, and continue. + } + Err(e) => return Err(e.into()), + } let prompt = format!( "{} {} {} {} {}",