Skip to content

Use track_caller for SpawnExt (and maybe LocalSpawnExt) #2977

@opara-tor

Description

@opara-tor

The Spawn trait provides an abstract interface for spawning tasks. We use this in Arti to allow users to swap the underlying runtime between tokio, async-std, and smol. One problem that we've run into is that the SpawnExt helper interface does not work with tokio-console when using tokio. To provide the code source location of where a task was spawned, tokio relies on the #[track_caller] attribute being applied to the spawn methods. Since SpawnExt::spawn() doesn't use #[track_caller], tokio-console always reports the SpawnExt::spawn() as the source of the task. For example it will report something like:

⚠ This task has woken itself for more than 50% of its total wakeups (51%)
Location: <cargo>/futures-util-0.3.31/src/task/spawn.rs:53:14

If #[track_caller] is added to the SpawnExt::spawn() definition, then tokio-console works as expected:

⚠ This task has woken itself for more than 50% of its total wakeups (51%)
Location: /tmp/arti/crates/tor-circmgr/src/lib.rs:522:14

We can't add #[track_caller] ourselves because we can't provide our own impl of SpawnExt (trait coherence).

I think it would be useful to add #[track_caller] to SpawnExt::spawn() and SpawnExt::spawn_with_handle(). And possibly for LocalSpawnExt as well.

Would you be interested in this change? If so are there any futures-util 0.3.x releases on the 0.3 branch planned that we could add this to? Or should this go on main only?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions