History

Fix #85

Sebastian Thiel committed 5 years ago

Commit 39fb6121 parent b792ff77 files · +58 −3Browse files

Unified · Split

7 files
  1. src/porcelain/main.rs +9 −0
  2. src/porcelain/options.rs +2 −0
  3. src/shared.rs +18 −3
  4. tests/snapshots/panic-behaviour/expected-failure +2 −0
  5. tests/snapshots/panic-behaviour/expected-failure-in-thread +2 −0
  6. tests/snapshots/panic-behaviour/expected-failure-in-thread-with-progress +3 −0
  7. tests/stateless-journey.sh +22 −0
src/porcelain/main.rs+9−0
@@ -13,6 +13,15 @@ pub fn main() -> Result<()> { · 12 unmodified lines
1313 let progress_keep_open = args.progress_keep_open;
1414
1515 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+ ),
1625 Subcommands::Init { directory } => core::repository::init(directory),
1726 Subcommands::Tools(tool) => match tool {
1827 ToolCommands::EstimateHours(EstimateHours {
src/porcelain/options.rs+2−0
@@ -40,6 +40,8 @@ pub enum Subcommands { · 39 unmodified lines
4040 #[clap(setting = AppSettings::ColoredHelp, setting = AppSettings::DisableVersion, setting = AppSettings::SubcommandRequired)]
4141 #[clap(visible_alias = "t")]
4242 Tools(ToolCommands),
43+ #[cfg(debug_assertions)]
44+ Panic,
4345 }
4446
4547 #[derive(Debug, Clap)]
src/shared.rs+18−3
@@ -91,6 +91,7 @@ pub mod pretty { · 90 unmodified lines
9191 (true, false) => {
9292 enum Event<T> {
9393 UiDone,
94+ ComputationFailed,
9495 ComputationDone(Result<T>),
9596 }
9697 let progress = crate::shared::progress_tree();
@@ -109,19 +110,33 @@ pub mod pretty { · 12 unmodified lines
109110 });
110111 // LIMITATION: This will hang if the thread panics as no message is send and the renderer thread will wait forever.
111112 // `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+ }
115124 });
116125 match rx.recv()? {
117126 Event::UiDone => {
118127 ui_handle.shutdown_and_wait();
128+ drop(join_handle);
119129 Err(anyhow!("Operation cancelled by user"))
120130 }
121131 Event::ComputationDone(res) => {
122132 ui_handle.shutdown_and_wait();
133+ join_handle.join().ok();
123134 res
124135 }
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+ },
125140 }
126141 }
127142 (true, true) | (false, true) => {
tests/snapshots/panic-behaviour/expected-failure+2−0
@@ -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
tests/snapshots/panic-behaviour/expected-failure-in-thread+2−0
@@ -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
tests/snapshots/panic-behaviour/expected-failure-in-thread-with-progress+3−0
@@ -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
tests/stateless-journey.sh+22−0
@@ -80,6 +80,28 @@ function small-repo-in-sandbox() { · 79 unmodified lines
8080 if test "$kind" = "max"; then
8181 title "Porcelain ${kind}"
8282 (
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+ )
83105 snapshot="$snapshot/porcelain"
84106 (with_program tree
85107 (when "using the 'tools' subcommand"

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t