Unified · Split
| @@ -13,6 +13,15 @@ pub fn main() -> Result<()> { · 12 unmodified lines | ||
| 13 | 13 | let progress_keep_open = args.progress_keep_open; |
| 14 | 14 | |
| 15 | 15 | match args.cmd { |
| 16 | + #[cfg(debug_assertions)] | |
| 17 | + Subcommands::Panic => prepare_and_run( | |
| 18 | + "panic-behaviour", | |
| 19 | + verbose, | |
| 20 | + progress, | |
| 21 | + progress_keep_open, | |
| 22 | + crate::shared::STANDARD_RANGE, | |
| 23 | + move |_progress, _out, _err| panic!("something went very wrong"), | |
| 24 | + ), | |
| 16 | 25 | Subcommands::Init { directory } => core::repository::init(directory), |
| 17 | 26 | Subcommands::Tools(tool) => match tool { |
| 18 | 27 | ToolCommands::EstimateHours(EstimateHours { |
| @@ -40,6 +40,8 @@ pub enum Subcommands { · 39 unmodified lines | ||
| 40 | 40 | #[clap(setting = AppSettings::ColoredHelp, setting = AppSettings::DisableVersion, setting = AppSettings::SubcommandRequired)] |
| 41 | 41 | #[clap(visible_alias = "t")] |
| 42 | 42 | Tools(ToolCommands), |
| 43 | + #[cfg(debug_assertions)] | |
| 44 | + Panic, | |
| 43 | 45 | } |
| 44 | 46 | |
| 45 | 47 | #[derive(Debug, Clap)] |
| @@ -91,6 +91,7 @@ pub mod pretty { · 90 unmodified lines | ||
| 91 | 91 | (true, false) => { |
| 92 | 92 | enum Event<T> { |
| 93 | 93 | UiDone, |
| 94 | + ComputationFailed, | |
| 94 | 95 | ComputationDone(Result<T>), |
| 95 | 96 | } |
| 96 | 97 | let progress = crate::shared::progress_tree(); |
| @@ -109,19 +110,33 @@ pub mod pretty { · 12 unmodified lines | ||
| 109 | 110 | }); |
| 110 | 111 | // LIMITATION: This will hang if the thread panics as no message is send and the renderer thread will wait forever. |
| 111 | 112 | // `catch_unwind` can't be used as a parking lot mutex is not unwind safe, coming from prodash. |
| 112 | − std::thread::spawn(move || { | |
| 113 | − let res = run(Some(sub_progress), &mut stdout(), &mut stderr()); | |
| 114 | − tx.send(Event::ComputationDone(res)).ok(); | |
| 113 | + let join_handle = std::thread::spawn(move || { | |
| 114 | + let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { | |
| 115 | + run(Some(sub_progress), &mut stdout(), &mut stderr()) | |
| 116 | + })); | |
| 117 | + match res { | |
| 118 | + Ok(res) => tx.send(Event::ComputationDone(res)).ok(), | |
| 119 | + Err(err) => { | |
| 120 | + tx.send(Event::ComputationFailed).ok(); | |
| 121 | + std::panic::resume_unwind(err) | |
| 122 | + } | |
| 123 | + } | |
| 115 | 124 | }); |
| 116 | 125 | match rx.recv()? { |
| 117 | 126 | Event::UiDone => { |
| 118 | 127 | ui_handle.shutdown_and_wait(); |
| 128 | + drop(join_handle); | |
| 119 | 129 | Err(anyhow!("Operation cancelled by user")) |
| 120 | 130 | } |
| 121 | 131 | Event::ComputationDone(res) => { |
| 122 | 132 | ui_handle.shutdown_and_wait(); |
| 133 | + join_handle.join().ok(); | |
| 123 | 134 | res |
| 124 | 135 | } |
| 136 | + Event::ComputationFailed => match join_handle.join() { | |
| 137 | + Ok(_) => unreachable!("The thread has panicked and unwrap is expected to show it"), | |
| 138 | + Err(err) => std::panic::resume_unwind(err), | |
| 139 | + }, | |
| 125 | 140 | } |
| 126 | 141 | } |
| 127 | 142 | (true, true) | (false, true) => { |
| @@ -0,0 +1,2 @@ | ||
| 1 | +thread 'main' panicked at 'something went very wrong', src/porcelain/main.rs:23:42 | |
| 2 | +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | |
| @@ -0,0 +1,2 @@ | ||
| 1 | +thread '<unnamed>' panicked at 'something went very wrong', src/porcelain/main.rs:23:42 | |
| 2 | +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | |
| @@ -0,0 +1,3 @@ | ||
| 1 | +[?1049h[?25lthread '<unnamed>' panicked at 'something went very wrong', src/porcelain/main.rs:23:42 | |
| 2 | +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | |
| 3 | +[?25h[?1049lError: receiving on a closed channel | |
| @@ -80,6 +80,28 @@ function small-repo-in-sandbox() { · 79 unmodified lines | ||
| 80 | 80 | if test "$kind" = "max"; then |
| 81 | 81 | title "Porcelain ${kind}" |
| 82 | 82 | ( |
| 83 | + (when "running a debug-only panic test" | |
| 84 | + snapshot="$snapshot/panic-behaviour" | |
| 85 | + (with "the --quiet option set" | |
| 86 | + it "fails as expected" && { | |
| 87 | + WITH_SNAPSHOT="$snapshot/expected-failure" \ | |
| 88 | + expect_run_sh 101 "$exe -q panic" | |
| 89 | + } | |
| 90 | + ) | |
| 91 | + | |
| 92 | + (with "NO --quiet option set" | |
| 93 | + it "fails as expected" && { | |
| 94 | + WITH_SNAPSHOT="$snapshot/expected-failure-in-thread" \ | |
| 95 | + expect_run_sh 101 "$exe panic" | |
| 96 | + } | |
| 97 | + ) | |
| 98 | + (with "progress option set" | |
| 99 | + it "fails as expected" && { | |
| 100 | + WITH_SNAPSHOT="$snapshot/expected-failure-in-thread-with-progress" \ | |
| 101 | + expect_run_sh $WITH_FAILURE "$exe --progress panic" | |
| 102 | + } | |
| 103 | + ) | |
| 104 | + ) | |
| 83 | 105 | snapshot="$snapshot/porcelain" |
| 84 | 106 | (with_program tree |
| 85 | 107 | (when "using the 'tools' subcommand" |