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

Add on_spawn configuration for Builder #3181

Closed
malaire opened this issue Nov 26, 2020 · 3 comments · Fixed by #6742
Closed

Add on_spawn configuration for Builder #3181

malaire opened this issue Nov 26, 2020 · 3 comments · Fixed by #6742
Assignees
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-runtime Module: tokio/runtime

Comments

@malaire
Copy link

malaire commented Nov 26, 2020

Is your feature request related to a problem? Please describe.

For monitoring and tracking purposes I want to run some custom code when any spawned task is created/polled/dropped.

Describe the solution you'd like

I propose a configuration method on_spawn for Builder which would take a wrapper function to be applied to all spawned tasks.

For example following two examples would be equivalent:

(First example in playground, with example Wrapper)

fn main() {
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            tokio::spawn(Wrapper::new(async {
                println!("Hello world")
            })).await.unwrap();
        });
}
fn main() {
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .on_spawn(Wrapper::new)
        .build()
        .unwrap()
        .block_on(async {
            tokio::spawn(async {
                println!("Hello world")
            }).await.unwrap();
        });
}

Describe alternatives you've considered

Alternative solution would be to apply this wrapper in all places where task is spawned. While this is possible for my own code, and also for crates like Hyper which offer configuration option like with_executor, this is not possible when using crates which do not offer such configuration option.

Doing this configuration for tokio runtime would be much better option.

@malaire malaire added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Nov 26, 2020
@Darksonn Darksonn added the M-runtime Module: tokio/runtime label Nov 26, 2020
@MikailBag
Copy link
Contributor

As a downside, such an on_spawn callback can't be generic (unless the whole tokio Runtime is generic), so one additional level of indirection is added to each poll.

@malaire
Copy link
Author

malaire commented Nov 27, 2020

Also I'd like similar on_spawn_blocking which would apply to spawn_blocking. Both wrappers would run "in context" of parent task to have access to any task-locals of parent task.

@malaire
Copy link
Author

malaire commented Nov 27, 2020

@MikailBag In my use cases I'm adding some code to each poll so that indirection is required.

If there are other use cases which would only run some code on each spawn, but not on each poll, then non-wrapping callback would be better. Perhaps that could be called on_task_start, like on_thread_start?

@Noah-Kennedy Noah-Kennedy self-assigned this Jul 31, 2024
Noah-Kennedy added a commit that referenced this issue Aug 1, 2024
This change implements two hooks for per-task actions, one which is invoked on task spawn, and one which is invoked during task termination.

These hooks initially are only supplied with the task ID (on unstable only), but more information can be added in the future, as the struct used to supply parameters is opaque.

Fixes #3181.
Noah-Kennedy added a commit that referenced this issue Aug 1, 2024
This change implements two hooks for per-task actions, one which is invoked on task spawn, and one which is invoked during task termination.

These hooks initially are only supplied with the task ID (on unstable only), but more information can be added in the future, as the struct used to supply parameters is opaque.

Fixes #3181.
Noah-Kennedy added a commit that referenced this issue Aug 1, 2024
This change implements two hooks for per-task actions, one which is invoked on task spawn, and one which is invoked during task termination.

These hooks initially are only supplied with the task ID (on unstable only), but more information can be added in the future, as the struct used to supply parameters is opaque.

Fixes #3181.
Noah-Kennedy added a commit that referenced this issue Aug 1, 2024
This change implements two hooks for per-task actions, one which is invoked on task spawn, and one which is invoked during task termination.

These hooks initially are only supplied with the task ID (on unstable only), but more information can be added in the future, as the struct used to supply parameters is opaque.

Fixes #3181.
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. M-runtime Module: tokio/runtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants