Skip to content

Tracking issue for LocalRuntime stabilization #7558

@ADD-SP

Description

@ADD-SP

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 LocalSet can tokio::spawn to get onto the runtime, but once you're on the runtime, you can no longer spawn_local to get back into the LocalSet.
  • From the runtime's perspective, all of the tasks in the LocalSet behave like a single task using FuturesUnordered, so various runtime options such as event_interval behave in > a very surprising way by counting many tasks at once.
  • After discussions with Deno, the LocalSet has been shown to involve considerable performance overhead compared to unsafely spawning the !Send tasks onto a current-thread runtime.

  • Issue Add on_spawn configuration 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.

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.

New features

The scope includes of tokio, tokio-macros, tokio-stream, tokio-util, and tokio-test.

Others

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 .

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateA-tokio-utilArea: The tokio-util crate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions