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

Identify if thread being created is a worker or blocking in thread_name_fn #6646

Open
dinhani opened this issue Jun 17, 2024 · 1 comment
Open
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-runtime Module: tokio/runtime

Comments

@dinhani
Copy link

dinhani commented Jun 17, 2024

Is your feature request related to a problem? Please describe.
No. It is something that will help us debug code identifying when blocking code that should always be executed with spawn_blocking is being executed by worker threads and blocking all the other futures.

Describe the solution you'd like
thread_name_fn should receive a parameter to indicate if the thread being created is a working thread or a blocking thread.

Alternatively we could have a method for worker threads and another one for blocking threads.

Describe alternatives you've considered

I am using this workaround to achieve the desired behavior, but it is not very robust.

.thread_name_fn(move || {
    // Tokio first create all async threads, then all blocking threads.
    // Threads are not expected to die because Tokio catches panics and blocking threads are configured to never die.
    // If one of these premises are not true anymore, this will possibly categorize threads wrongly.
    
    static ASYNC_ID: AtomicUsize = AtomicUsize::new(1);
    static BLOCKING_ID: AtomicUsize = AtomicUsize::new(1);
    
    // identify async threads
    let async_id = ASYNC_ID.fetch_add(1, Ordering::SeqCst);
    if async_id <= num_async_threads {
        return format!("tokio-async-{}", async_id);
    }
    
    // identify blocking threads
    let blocking_id = BLOCKING_ID.fetch_add(1, Ordering::SeqCst);
    format!("tokio-blocking-{}", blocking_id)
})             

Additional context
...

@dinhani dinhani added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Jun 17, 2024
@dinhani dinhani changed the title Identify if thread being created is a worker or blocing in thread_name_fn Identify if thread being created is a worker or blocking in thread_name_fn Jun 17, 2024
@mox692 mox692 added the M-runtime Module: tokio/runtime label Jun 18, 2024
@Darksonn
Copy link
Contributor

Unfortunately, if block_in_place is being used, a thread that used to be a blocking thread can become a worker thread, and vice-versa.

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

No branches or pull requests

3 participants