Skip to content

Commit

Permalink
tree-wide: continue unwinds on join failures
Browse files Browse the repository at this point in the history
The error from joining a thread is a bit confusing. It is only printed
if the other thread panicked. This means, effectively, we only get here
if something called .unwrap(), .expect() or panicked in a different way.
In these cases an (ugly) error was already printend. Printing a pretty
message about the join failure does not really help a lot...

So let's just continue the unwind as suggested by the docs on the join
`Result` [1].

Before:

    thread '<unnamed>' panicked at 'Test panic', crates/gpio/src/backend.rs:146:13
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Any { .. }', crates/gpio/src/backend.rs:176:23

After:

    thread '<unnamed>' panicked at 'Test panic', crates/gpio/src/backend.rs:146:13
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

[1]: https://doc.rust-lang.org/std/thread/type.Result.html

Signed-off-by: Erik Schilling <[email protected]>
  • Loading branch information
Ablu committed Nov 14, 2023
1 parent a5dbada commit 2bd4493
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
4 changes: 1 addition & 3 deletions vhost-device-gpio/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ pub(crate) enum Error {
DeviceDuplicate(u32),
#[error("Failed while parsing to integer: {0:?}")]
ParseFailure(ParseIntError),
#[error("Failed to join threads")]
FailedJoiningThreads,
#[error("Could not open gpio device: {0}")]
CouldNotOpenDevice(crate::gpio::Error),
#[error("Could not create gpio controller: {0}")]
Expand Down Expand Up @@ -227,7 +225,7 @@ fn start_backend(args: GpioArgs) -> Result<()> {
}

for handle in handles {
handle.join().map_err(|_| Error::FailedJoiningThreads)??;
handle.join().map_err(std::panic::resume_unwind).unwrap()?;
}

Ok(())
Expand Down
4 changes: 1 addition & 3 deletions vhost-device-i2c/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ pub(crate) enum Error {
I2cFailure(i2c::Error),
#[error("Failed while parsing to integer: {0:?}")]
ParseFailure(ParseIntError),
#[error("Failed to join threads")]
FailedJoiningThreads,
#[error("Could not create backend: {0}")]
CouldNotCreateBackend(vhu_i2c::Error),
#[error("Could not create daemon: {0}")]
Expand Down Expand Up @@ -215,7 +213,7 @@ fn start_backend<D: 'static + I2cDevice + Send + Sync>(args: I2cArgs) -> Result<
}

for handle in handles {
handle.join().map_err(|_| Error::FailedJoiningThreads)??;
handle.join().map_err(std::panic::resume_unwind).unwrap()?;
}

Ok(())
Expand Down
4 changes: 1 addition & 3 deletions vhost-device-rng/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ pub(crate) enum Error {
InvalidPeriodInput(u128),
#[error("Wrong socket count: {0}")]
InvalidSocketCount(u32),
#[error("Threads can't be joined")]
FailedJoiningThreads,
#[error("Could not create backend: {0}")]
CouldNotCreateBackend(std::io::Error),
#[error("Could not create daemon: {0}")]
Expand Down Expand Up @@ -139,7 +137,7 @@ pub(crate) fn start_backend(config: VuRngConfig) -> Result<()> {
}

for handle in handles {
handle.join().map_err(|_| Error::FailedJoiningThreads)??;
handle.join().map_err(std::panic::resume_unwind).unwrap()?;
}

Ok(())
Expand Down
7 changes: 6 additions & 1 deletion vhost-device-vsock/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,12 @@ pub(crate) fn start_backend_servers(configs: &[VsockConfig]) -> Result<(), Backe

while !handles.is_empty() {
let thread_id = receiver.recv().unwrap();
handles.remove(&thread_id).unwrap().join().unwrap()?;
handles
.remove(&thread_id)
.unwrap()
.join()
.map_err(std::panic::resume_unwind)
.unwrap()?;
}

Ok(())
Expand Down

0 comments on commit 2bd4493

Please sign in to comment.