-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
This is the central place to track all relevant works about stabilizing the LocalRuntime.
What is the LocalRuntime?
Currrntly, there are two types of stable runtime supported by tokio, Current Thread Runtime and Multi Thread Runtime, LocalRuntime is the thrid type of runtime, which allows downstream to spawn !Send Futures.
Why do we need the LocalRuntime?
The tokio::spawn and similar functions require the Future can be Send. However, it is reasonable to spawn a !Send Future in certain cases. While the Current Thread Runtime running all tasks in a single thread, spawning local async task is still not ergonomic and straightforward, and also has some issues (see below).
What did we try?
We introduced LocalSet in #1733, which allows downstream to spawn !Send Futures. However, there are some downsides.
- Tasks on the
LocalSetcantokio::spawnto get onto the runtime, but once you're on the runtime, you can no longerspawn_localto get back into theLocalSet.- From the runtime's perspective, all of the tasks in the
LocalSetbehave like a single task usingFuturesUnordered, so various runtime options such asevent_intervalbehave in > a very surprising way by counting many tasks at once.- After discussions with Deno, the
LocalSethas been shown to involve considerable performance overhead compared to unsafely spawning the!Sendtasks onto a current-thread runtime.
- Issue Add
on_spawnconfiguration for Builder #3181 proposes to add hooks that run when a task is spawned / destroyed. The hooks would not run for tasks on the LocalSet. This is confusing.- Task dumps currently don't look inside block_on, which means that LocalSet tasks are completely invisible to task dumps.
Ref: #6739
So we need another solution.
Steps to stabilize the LocalRuntime
In high-level, there are several areas, and we need to distinguish between tasks that must be completed before stabilization and those that can be completed after stabilization.
Analyze the potential impacts on the existing usage.
The scope should at least includes existing usage of tokio, tokio-macros, tokio-stream, tokio-util, and tokio-test. We may need to deprecate the existing features.
- Proposal: Deprecate Tokio's LocalSet #6741
- RFC: Replace the internal implementation of
LocalPoolHandlewithLocalRuntime#7560 - More ... (please leave comment in this issue to rich this list)
Change the document and guideline
The scope includes the docs of tokio, tokio-macros, tokio-stream, tokio-util, tokio-test, and https://tokio.rs/tokio/tutorial.
- macros: add "local" runtime flavor #7375
- Guideline for choosing between Current Thread Runtime and Local Runtime #7559
- Clarify the behavior of several
spawn_localmethods #7561 - More ... (please leave comment in this issue to rich this list)
New features
The scope includes of tokio, tokio-macros, tokio-stream, tokio-util, and tokio-test.
- rt: add thread (un)parking hooks for
LocalRuntime#7420 - Do we need a
tokio::spawn_localas the shortcut of [tokio::task::spawn_local]? (needs a dedicated issue) - More ... (please leave comment in this issue to rich this list)
Others
- tokio: Stabilize LocalRuntime #7557 (removes the
cfg(tokio_unstable)gate) - Improve the test coverage of task containers on
LocalRuntime#7562 - More ... (please leave comment in this issue to rich this list)
FAQs
Shall we deprecate the Current Thread Runtime?
No, we shouldn't, because it has valid use cases. See https://tokio.rs/tokio/topics/bridging and #7559 .