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

Consolidate errors for context missing #3441

Merged
merged 38 commits into from Jan 20, 2021

Conversation

aknuds1
Copy link
Contributor

@aknuds1 aknuds1 commented Jan 17, 2021

Motivation

Standardize the error messages for when the Tokio context hasn't been instantiated or required properties on the context are disabled, fixes #3361.

Solution

The solution wrt. the context not being instantiated is to add constant util::error::CONTEXT_MISSING_ERROR, that is used instead of hardcoding the Tokio context missing error message.

Happy for any feedback, e.g., if I overlooked something.

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1 aknuds1 marked this pull request as ready for review January 17, 2021 11:45
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
tokio/src/task/spawn.rs Outdated Show resolved Hide resolved
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
tokio/src/util/error.rs Outdated Show resolved Hide resolved
aknuds1 and others added 3 commits January 17, 2021 14:34
Co-authored-by: Alice Ryhl <alice@ryhl.io>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1 aknuds1 requested a review from Darksonn January 17, 2021 15:45
@aknuds1
Copy link
Contributor Author

aknuds1 commented Jan 17, 2021

@Darksonn tried to address your feedback.

crate::runtime::context::io_handle()
.expect("there is no reactor running, must be called from the context of Tokio runtime")
crate::runtime::context::io_handle().expect("io not enabled on Tokio context")
Copy link
Contributor

Choose a reason for hiding this comment

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

This error should check whether there is a runtime context or not. If there is, it should fail with an error saying that there is a runtime, but IO is disabled on it. Otherwise it should fail with the same error as the other panics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Darksonn I made runtime::context::io_handle() fail with the standard error if the Tokio context isn't instantiated, does this address your concern or should it be implemented differently?

If runtime::context::io_handle() shouldn't panic in case of missing Tokio context, the return value has to differentiate between missing context and io being disabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't tell there being any technical drawback to have runtime::context::io_handle() panic if the Tokio context is missing.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should panic in both cases, but the error messages should be different.

Copy link
Contributor Author

@aknuds1 aknuds1 Jan 17, 2021

Choose a reason for hiding this comment

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

@Darksonn I'm wondering if there might be a misunderstanding; the way I've implemented it, there are different error messages depending on whether the Tokio context is missing or io isn't enabled on it.

I implemented it so that runtime::context::io_handle will panic if the context is missing. Otherwise, io::driver::Handle::current will panic if the context doesn't have io enabled.

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

I want a test for each of these cases:

  • A timer was used, but no runtime is present.
  • A timer was used, but the runtime has timers disabled.
  • IO was used, but not runtime is present.
  • IO was used, but the runtime has IO disabled.

You only have some of these cases.

tokio/tests/no_rt.rs Outdated Show resolved Hide resolved
tokio/src/time/driver/handle.rs Outdated Show resolved Hide resolved
@@ -71,8 +71,7 @@ cfg_not_rt! {
/// lazy, and so outside executed inside the runtime successfuly without
/// panicking.
pub(crate) fn current() -> Self {
panic!("there is no timer running, must be called from the context of Tokio runtime or \
`rt` is not enabled")
panic!("`rt` must be enabled")
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be using the standardized error message, no?

Copy link
Contributor Author

@aknuds1 aknuds1 Jan 17, 2021

Choose a reason for hiding this comment

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

@Darksonn Isn't the difference here that the rt feature is disabled? It isn't just that the context hasn't been instantiated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, here the rt feature needs to be enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Darksonn but you're suggesting this error message should change? I'm not following.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you provide a code change suggestion, if you have something in mind?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure.

panic!(context_missing_error(&["rt"]));

In fact, you do not currently use the list argument anywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Darksonn OK, I figured it was kind of pointless to produce an error saying that there's no reactor running since the runtime isn't compiled in in the first place (due to the "rt" feature being disabled).

Copy link
Contributor

Choose a reason for hiding this comment

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

You are free to pick another wording, but "rt must be enabled" is too short. It needs to mention that this is a Tokio feature, including the Tokio version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Darksonn Please see my updated error string.

aknuds1 and others added 5 commits January 17, 2021 18:05
Co-authored-by: Alice Ryhl <alice@ryhl.io>
Co-authored-by: Alice Ryhl <alice@ryhl.io>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1
Copy link
Contributor Author

aknuds1 commented Jan 17, 2021

I want a test for each of these cases:

  • A timer was used, but no runtime is present.
  • A timer was used, but the runtime has timers disabled.
  • IO was used, but not runtime is present.
  • IO was used, but the runtime has IO disabled.

@Darksonn I should have tests for the first two cases. How can I write corresponding tests for io? I'm not sure how to trigger the panic wrt. io.

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1 aknuds1 changed the title Use function to produce error string for context missing Consolidate errors for context missing Jan 18, 2021
@aknuds1
Copy link
Contributor Author

aknuds1 commented Jan 18, 2021

@Darksonn Made it into a constant string.

tokio/src/io/driver/mod.rs Outdated Show resolved Hide resolved
@@ -274,7 +273,7 @@ cfg_not_rt! {
/// This function panics if there is no current reactor set, or if the `rt`
/// feature flag is not enabled.
pub(super) fn current() -> Self {
panic!("there is no reactor running, must be called from the context of Tokio runtime with `rt` enabled.")
panic!("feature `rt` must be enabled on Tokio 1.x")
Copy link
Contributor

Choose a reason for hiding this comment

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

Honestly, I think using the standardized error message here too would be fine. I mean sure, you need to enable rt to fix this error, but the root of the problem is that there is no runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Darksonn OK, I might do that. I kind of see it the other way around though I guess, i.e. that the root of problem is that the "rt" feature is disabled.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean, if you enable the rt feature, you're just going to get the other error about a runtime missing. 🤷‍♀️

Copy link
Contributor Author

@aknuds1 aknuds1 Jan 18, 2021

Choose a reason for hiding this comment

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

Yeah. I'll tweak the message when I finish work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

tokio/src/time/driver/handle.rs Outdated Show resolved Hide resolved
@@ -71,8 +71,7 @@ cfg_not_rt! {
/// lazy, and so outside executed inside the runtime successfuly without
/// panicking.
pub(crate) fn current() -> Self {
panic!("there is no timer running, must be called from the context of Tokio runtime or \
`rt` is not enabled")
panic!("feature `rt` must be enabled on Tokio 1.x")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here as before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@aknuds1 aknuds1 requested a review from Darksonn January 19, 2021 18:57
@@ -274,7 +273,7 @@ cfg_not_rt! {
/// This function panics if there is no current reactor set, or if the `rt`
/// feature flag is not enabled.
pub(super) fn current() -> Self {
panic!("there is no reactor running, must be called from the context of Tokio runtime with `rt` enabled.")
panic!(&format!("{} with feature `rt` enabled", crate::util::error::CONTEXT_MISSING_ERROR))
Copy link
Contributor

Choose a reason for hiding this comment

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

Still, I mean, you can't have a runtime without enabling rt, but ok. This is fine with me.

Suggested change
panic!(&format!("{} with feature `rt` enabled", crate::util::error::CONTEXT_MISSING_ERROR))
panic!("{} with feature `rt` enabled", crate::util::error::CONTEXT_MISSING_ERROR)

Copy link
Contributor Author

@aknuds1 aknuds1 Jan 19, 2021

Choose a reason for hiding this comment

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

@Darksonn I actually didn't think about that, switched to just crate::util::error::CONTEXT_MISSING_ERROR instead.

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1 aknuds1 requested a review from Darksonn January 19, 2021 19:47
aknuds1 and others added 3 commits January 19, 2021 20:53
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Co-authored-by: Alice Ryhl <alice@ryhl.io>
@aknuds1
Copy link
Contributor Author

aknuds1 commented Jan 20, 2021

@Darksonn committed your suggestions.

@Darksonn
Copy link
Contributor

We have some tests to update.

@aknuds1
Copy link
Contributor Author

aknuds1 commented Jan 20, 2021

We have some tests to update.

Ah yeah of course. Will take care of it.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Thank you for the PR!

@Darksonn Darksonn merged commit fdde558 into tokio-rs:master Jan 20, 2021
@aknuds1 aknuds1 deleted the chore/context-missing branch January 20, 2021 13:31
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime labels Jan 22, 2021
@Darksonn Darksonn mentioned this pull request Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Standardize missing executor panic messages
2 participants