Skip to content
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

fix: improves panic messages when there is no reactor/timer #2145

Merged

Conversation

EverlastingBugstopper
Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper commented Jan 21, 2020

This PR replaces #1789

I want to preface this by saying I'm not entirely sure if these error messages are exactly accurate but I wanted to take a crack at it.


Motivation

Providing a more actionable error message when tokio expects to be within a runtime. Fixes #1713

Solution

New error messages:

When there is no timer:

the timer is shutdown, this code must be executed from within a tokio runtime

When there is no reactor:

there is no reactor running, this code must be executed from within a tokio runtime

I'm not in love with the "this code must be executed from within a tokio runtime", not sure what the best wording there is, would love some pointers :) I'm also not sure if it's entirely accurate to say "tokio runtime" as perhaps some other runtime could be used instead.

Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic! I think we just need to update one more message first.

@@ -64,7 +64,9 @@ impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::Kind::*;
let descr = match self.0 {
Shutdown => "timer is shutdown",
Shutdown => {
"the timer is shutdown, this code must be executed from within a tokio runtime"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is great, I think though this is actually the place where we want to add the panic message as well https://github.com/tokio-rs/tokio/blob/master/tokio/src/time/driver/handle.rs#L24

I am also curious if this is something we could test? maybe via a panic handler?

Copy link
Contributor Author

@EverlastingBugstopper EverlastingBugstopper Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @LucioFranco - I've added the other panic, and also updated the error messages to match a similar panic code I found elsewhere in the code base. There are now 4 panic messages that include must be called from the context of Tokio runtime

I also added one test case for when there is no timer, but I'm having a hard time reproducing the other panic messages. I think the tests should look very similar to the one I have written, I just can't seem to speak the magic incantation to get it to actually panic that way.

let _ = timeout(dur, rx).await;
}

// #[test]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we can remove this commented out test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can - i just included it for now to show my thought process for such a test. like i said above - not quite sure how to test the other panic types, only one i could get was a timer panic

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yup, sorry I totally missed that and went to the code right away!

So I think it should be possible to catch the io driver panic if you try to tcp connect? If not I am fine not testing all of them for now. If that is the case id like to remove the commented out test then we can merge this. Thanks!

@EverlastingBugstopper EverlastingBugstopper force-pushed the avery/fix-panic-messages branch 2 times, most recently from e68f43b to 058121e Compare January 22, 2020 23:35
Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you! This is def something we should have done a long time ago :)

@EverlastingBugstopper
Copy link
Contributor Author

Ready to go!

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👍 thanks.

@carllerche carllerche merged commit 9eca96a into tokio-rs:master Jan 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve panic messages when runtime is not running
3 participants