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

Introspect runtime flavor/configuration on Handle #5088

Closed
mooso opened this issue Oct 10, 2022 · 6 comments · Fixed by #5138
Closed

Introspect runtime flavor/configuration on Handle #5088

mooso opened this issue Oct 10, 2022 · 6 comments · Fixed by #5138
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. E-easy Call for participation: Experience needed to fix: Easy / not much M-runtime Module: tokio/runtime

Comments

@mooso
Copy link

mooso commented Oct 10, 2022

Some functions in tokio, such as block_in_place(), only work in a multi-threaded runtime. It would be great to be able to inspect the current configuration/flavor of the runtime so that we can conditionally call such functions, e.g.:

if Handle::current().is_multi_threaded() {
    block_in_place(move || { long_running_code(...) })
} else {
    long_running_code(...)
}
@mooso mooso added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Oct 10, 2022
@carllerche
Copy link
Member

I'm fine w/ this. Handle is a pretty safe place to put these fns.

@Darksonn Darksonn added the M-runtime Module: tokio/runtime label Oct 10, 2022
@Darksonn
Copy link
Contributor

I don't mind adding such a method, but I am worried about the example you are showing us. The only situation where block_in_place would panic is in a current-thread runtime or LocalSet, i.e. in places where it is not okay to block. In particular, it does not panic if used outside of a runtime. Because of this, it seems to me that any situation where your code has different behavior from just always using block_in_place is a situation where your code is incorrect.

@mooso
Copy link
Author

mooso commented Oct 10, 2022

I am probably missing something @Darksonn - why would it not be OK to run some long running code in a current-thread runtime? Sure any other scheduled tasks will be blocked from running until the long running code is done, but that's just a perf degradation not a correctness issue right? In my use case the real perf-sensitive scenarios will always run with multi-threaded runtime, but some integration tests will run on current-thread runtimes and I just need them to be correct, not necessarily fast.

@Noah-Kennedy
Copy link
Contributor

This is more about what is good vs bad practice than what is a bug or not, but in general any code that "blocks" the runtime is bad, and honestly moreso if it's in the current_thread runtime, which is very useful for perf-sensitive scenarios.

@Darksonn
Copy link
Contributor

While what you're saying is true, the perf degradation is generally so bad that its unacceptable in pretty much every situation, but I admit that tests are probably a counterexample to that.

@Noah-Kennedy
Copy link
Contributor

Noah-Kennedy commented Oct 11, 2022

Fair point.

@carllerche carllerche added the E-easy Call for participation: Experience needed to fix: Easy / not much label Oct 27, 2022
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 C-feature-request Category: A feature request. E-easy Call for participation: Experience needed to fix: Easy / not much M-runtime Module: tokio/runtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants