New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong panic message when #[timeout(...)]
is not expired
#171
Comments
Yeah... you are right. Thanks to report it. I don't know when I can publish the new version.... sorry |
Sure. I think you should also consider a downcast for |
Ok. |
Great! Thanks. |
Ok, I fixed the The only way that I found to catch the stack trace is to use something like described in https://stackoverflow.com/questions/69593235/how-to-get-panic-information-i-e-stack-trace-with-catch-unwind but it seams a little bit overkilled and it will not prevent the If you are ok I'm incline to close this ticket now.... If you have some ideas of how to move the panic from other thread to test thread I would really happy to hear you. |
Great thanks for your work. Oh I see. You want to preserve the backtrace, what's pretty cool. After a lot of searching and reading through the stdlib, I found a nice and simple solution. pub fn execute_with_timeout_sync<T: 'static + Send, F: FnOnce() -> T + Send + 'static>(
code: F,
timeout: Duration,
) -> T {
let (sender, receiver) = mpsc::channel();
let thread = if let Some(name) = thread::current().name() {
std::thread::Builder::new().name(name.to_string())
} else {
std::thread::Builder::new()
};
let handle = thread.spawn(move || sender.send(code())).unwrap();
match receiver.recv_timeout(timeout) {
Ok(result) => {
// Unwraps are safe because we got a result from the thread, which is not a `SendError`,
// and there was no panic within the thread which caused a disconnect.
handle.join().unwrap().unwrap();
result
}
Err(RecvTimeoutError::Timeout) => {
panic!("Timeout {:?} expired", timeout)
}
Err(RecvTimeoutError::Disconnected) => match handle.join() {
Ok(_) => unreachable!(),
Err(any) => {
panic::resume_unwind(any);
}
},
}
} Sorry, I've written this on the base of an older commit on the main branch, but I hope you get the idea. |
Wow!!! Seams great! Tomorrow I'll try it. Thanks
Il mer 14 dic 2022, 21:18 Joining7943 ***@***.***> ha scritto:
… Great thanks for your work.
Oh I see. You want to preserve the backtrace, what's pretty cool. After a
lot of searching and reading through the stdlib, I found a nice and simple
solution.
pub fn execute_with_timeout_sync<T: 'static + Send, F: FnOnce() -> T + Send + 'static>(
code: F,
timeout: Duration,) -> T {
let (sender, receiver) = mpsc::channel();
let thread = if let Some(name) = thread::current().name() {
std::thread::Builder::new().name(name.to_string())
} else {
std::thread::Builder::new()
};
let handle = thread.spawn(move || sender.send(code())).unwrap();
match receiver.recv_timeout(timeout) {
Ok(result) => {
// Unwraps are safe because we got a result from the thread, which is not a `SendError`,
// and there was no panic within the thread which caused a disconnect.
handle.join().unwrap().unwrap();
result
}
Err(RecvTimeoutError::Timeout) => {
panic!("Timeout {:?} expired", timeout)
}
Err(RecvTimeoutError::Disconnected) => match handle.join() {
Ok(_) => unreachable!(),
Err(any) => {
panic::resume_unwind(any);
}
},
}}
Sorry, I've written this on the base of an older commit on the main
branch, but I hope you get the idea.
—
Reply to this email directly, view it on GitHub
<#171 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA5Y34IMQWR2A7QGTDGN6CLWNITRDANCNFSM6AAAAAAS5BPNUM>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
Sure :) |
THX again |
In
rstest
s with the#[timeout(...)]
macro attribute, the panic messages in tests in which the timeout is not reached are replaced byTimeout ... expired
.The original panics are still readable because the timeout thread panicked, but it's very confusing to see a
Timeout ... expired
although the timeout didn't expire. Also, it's not possible to match against the original panic string for example with#[should_panic = "my assertion"]
Here's a short example:
The test code showing the wrong behavior
The test output of
cargo test
for thetest_me
test function:The correct test output shouldn't show the panic message of the
<unnamed>
thread and should showthread 'test_me' panicked at 'my error message'
instead ofthread 'test_me' panicked at 'Timeout 60s expired'
.If interested, I can submit a pr fixing this in the
execute_with_timeout_sync
function inrstest/rstest/src/timeout.rs
.The text was updated successfully, but these errors were encountered: