runtime: add worker_index() getter#7921
Merged
ADD-SP merged 5 commits intotokio-rs:masterfrom Mar 15, 2026
Merged
Conversation
519b1f2 to
ec5c64a
Compare
Contributor
Author
|
Yes I like that:
Will change, thanks for review! |
worker_id() getterworker_index() getter
ADD-SP
reviewed
Feb 24, 2026
Member
ADD-SP
left a comment
There was a problem hiding this comment.
Overall looks good to me, just one question.
By the way, please sync the changes from the base branch as we have recently fixed a bug which may causes CI failure.
9b760e7 to
88f3ca9
Compare
martin-g
reviewed
Mar 3, 2026
Add `tokio::runtime::worker_id()` which returns the 0-based index of the current runtime worker thread. The index matches the worker indices used by `RuntimeMetrics` (e.g. `worker_total_busy_duration`), making it possible to correlate per-worker metrics with specific tasks. Returns `None` from blocking threads, the `block_on` caller thread (on the multi-thread runtime), and non-Tokio threads. For the current-thread runtime, always returns `Some(0)`.
martin-g
approved these changes
Mar 5, 2026
rcoh
added a commit
to rcoh/tokio
that referenced
this pull request
Mar 5, 2026
Add a `tokio::runtime::worker_index()` function that returns the index of the current runtime worker thread. This is useful for per-worker sharding and diagnostics. Squashed from tokio-rs#7921
rcoh
added a commit
to rcoh/tokio
that referenced
this pull request
Mar 5, 2026
Add a `tokio::runtime::worker_index()` function that returns the index of the current runtime worker thread. This is useful for per-worker sharding and diagnostics. Squashed from tokio-rs#7921
ADD-SP
approved these changes
Mar 15, 2026
Member
ADD-SP
left a comment
There was a problem hiding this comment.
Looks good to me. Synced with the base branch via the GitHub web UI—let’s see if CI is green.
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Motivation
I am working on some runtime tracing instrumentation and one of the current pain points is being able to figure out what worker is currently running so that it can be properly correlated with the runtime metrics. I am working around this currently by enumerating
RuntimeMetrics::worker_thread_idbut that's obviously not ideal.Solution
Add
tokio::runtime::worker_id()which returns the 0-based index of the current runtime worker thread. The index matches the worker indices used byRuntimeMetrics(e.g.worker_total_busy_duration), making it possible to correlate per-worker metrics with specific tasks.Returns
Nonefrom blocking threads, theblock_oncaller thread (on the multi-thread runtime), and non-Tokio threads. For the current-thread runtime, always returnsSome(0).