-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
rt: move block_on to handle #3097
Conversation
Signed-off-by: Marc-Antoine Perennou <[email protected]>
Signed-off-by: Marc-Antoine Perennou <[email protected]>
Signed-off-by: Marc-Antoine Perennou <[email protected]>
Signed-off-by: Marc-Antoine Perennou <[email protected]>
Signed-off-by: Marc-Antoine Perennou <[email protected]>
Signed-off-by: Marc-Antoine Perennou <[email protected]>
(force-push was just a missing comma in the last commit because of a fail amend, 1s after initial push) I'm not sure everything is handled as it should, but at least the public API should be ok and the behaviour too |
Since, this patch isn't trivial and is a larger change than we'd like. We are not going to include this in 1.0. As for other solutions, one should be using |
How would you get a reference to the runtime when using the |
In this, case you will want to just create the runtime manually and use block_on. Its a bit more verbose but isn't that much of a change. You just won't be able to use |
I am fine with letting this iterate for a little while until people are satisfied with the implementation, but I do want to add that as a library attempting to use Tokio entirely behind the scenes, just using I definitely agree that in most cases threading the runtime through the app is probably what you want, but in general I think of the runtime as a somewhat global/implicit resource. Being able to get a handle to that in any location is extremely useful. :) |
@sunjay can you explain more why |
If you have hundreds of tests, not being able to use As explained on #3076 (comment) , not having a uniform way to get access to the current |
@LucioFranco Sure. I am using tokio behind the scenes in the turtle crate. (PR for updating to tokio 0.3.) The crate is targeting beginners to Rust, so we try to avoid additional complexity whenever we can. In the default "synchronous" mode of the crate, tokio is hidden entirely. The runtime is created implicitly the first time an asynchronous call is made. When users use the We could potentially rely on the So that's why As for where we'd need to use I don't actually think there is any way to get around this one without |
@stuhood while yes its unfortunate, for us to get 1.0 out we have needed to reduce surface area, and this is one of those places unfortunately. Its possible to fork @sunjay personally, I think its fair to ask users to run it within a future executing on the tokio runtime. That way you reduce the surface area. I think Handle/Handle::current is a smell for a design. The tokio futures should panic and provide a clear message as to why (something I think we can do even better). |
Are we getting |
Just chiming in -- I may be missing something -- but I think I need Handle::try_current + Handle::block_on in an application I'm working on where I'm implementing an externally defined non-async trait on a struct which has internal async types (e.g., TcpStream). |
Thanks for doing this. I think this change is fine. It looks like it is mostly shuffling things around. I don't see a problem w/ how this landed. Thoughts @tokio-rs/maintainers? I believe this is blocking some upgrades to 1.0. |
I resolved the merge conflicts. |
How does this handle the case where you shut down the runtime while a handle is blocking on something? I'm fine with whatever, but it is nice to know the answer. |
A very good point. What do we want to see here? |
My guess is, we want the shutdown logic to reclaim the runtime and terminate it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before I review this anymore, I am not sure this behavior is correct and there are no tests? This type of behavior is tricky and we should have tests checking all the cases.
/// Run a future to completion on the Tokio runtime. This is the | ||
/// runtime's entry point. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is incorrect, this is not the entry point but just an additional block_on
?
/// | ||
/// # Multi thread scheduler | ||
/// | ||
/// When the multi thread scheduler is used this will allow futures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would mention it will also run spawned tasks on the runtime worker threads.
/// | ||
/// # Current thread scheduler | ||
/// | ||
/// When the current thread scheduler is enabled `block_on` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this is the behavior of Runtime::block_on
I don't think this should be the behavior of Handle::block_on
? @carllerche What do you think? I don't think the handle's block on should be able to steal the runtime. In that case it is the same as Runtime
except it removes the ownership of the runtime type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, my initial thinking was that the handle should follow the Runtime's behavior, but it does complicate the shutdown logic as well as seems a bit weird given the "current_thread" name... Perhaps, block_on
in this case should just block the thread and enter the runtime context...
Also, the behavior should follow this (which we don't even have yet for spawn) #3548 where we return an error when the runtime goes away. A handle should not hold ownership of any of the scheduler components (like the task queue). |
@Darksonn we should return an error, somehow we'd need to wake up. Its complicated but in the case where the runtime goes away while you're blocking on. It will just hang and this is bad UX/hard to debug. |
I opened a draft PR w/ an alternate strategy: #3549. I'm interested in feedback on the pros / cons. |
Thanks for the initial work & getting the ball going on this one. Closing in favor of #3569, which should be close. |
Fixes #2965
Fixes #3096
One difference between
Runtime::block_on
andHandle::block_on
: the latter doesn't use theInner
fromBasicScheduler in case of
CurrentThreadconfiguration. Given the main use of
Handle::block_on`` is to be able to callblock_on
on the Runtime when using `#[tokio::main]`, it shouldn't matter though, given the `Inner` is already being used to `blobk_on` the `main`, thus is `None`.One potential behavioural change inRuntime::block_on
: the use ofBasicScheduler::inner
is not longer in the loop, so we'll only attempt at using it on the first pass. I cannot think of an easy workaround for this without bringing the scheduler inside of the Handle, making itHandle<P: Park>
which would make it a pain to carry I think.