Skip to content

7921: runtime: add worker_index() getter#70

Open
martin-augment wants to merge 3 commits intomasterfrom
pr-7921-2026-03-03-12-39-33
Open

7921: runtime: add worker_index() getter#70
martin-augment wants to merge 3 commits intomasterfrom
pr-7921-2026-03-03-12-39-33

Conversation

@martin-augment
Copy link
Copy Markdown
Owner

7921: To review by AI

rcoh and others added 3 commits February 27, 2026 12:10
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)`.
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Tokio runtime by providing a new API to identify the specific worker thread executing a task. This feature is particularly useful for advanced debugging, logging, or metrics collection, allowing developers to gain deeper insights into how their asynchronous tasks are distributed and processed across the runtime's thread pool.

Highlights

  • New worker_index() function: A new tokio::runtime::worker_index() function has been introduced, allowing users to retrieve the 0-based index of the current worker thread within a Tokio runtime. This function is marked as tokio_unstable.
  • Internal API for worker index retrieval: Internal worker_index() methods were added across context.rs, scheduler/mod.rs, and scheduler/multi_thread/worker.rs to propagate the worker index information from the scheduler to the public API.
  • Comprehensive testing: A new test file, rt_worker_index.rs, was added to thoroughly test the worker_index() function's behavior in multi-thread, current-thread, and blocking contexts, as well as outside of a runtime.
Changelog
  • tokio/src/runtime/context.rs
    • Added pub(crate) fn worker_index() -> Option<usize> to retrieve the worker index, guarded by #[cfg(tokio_unstable)].
  • tokio/src/runtime/mod.rs
    • Added pub fn worker_index() -> Option<usize> with comprehensive documentation and usage examples, exposing the worker index to users.
    • Integrated the new context::worker_index() call to provide the public API.
  • tokio/src/runtime/scheduler/mod.rs
    • Implemented pub(crate) fn worker_index(&self) -> Option<usize> to return the worker index based on the runtime flavor (current-thread or multi-thread), guarded by #[cfg(tokio_unstable)].
  • tokio/src/runtime/scheduler/multi_thread/worker.rs
    • Added pub(crate) fn worker_index(&self) -> usize to directly access the worker's internal index, guarded by #[cfg(tokio_unstable)].
  • tokio/tests/rt_worker_index.rs
    • Added worker_index_multi_thread test to check index validity in a multi-thread runtime.
    • Added worker_index_current_thread test to verify index for current-thread runtime.
    • Added worker_index_outside_runtime test to confirm None outside a runtime.
    • Added worker_index_matches_metrics_worker_thread_id test to ensure consistency with runtime metrics.
    • Added worker_index_from_spawn_blocking test to check behavior in blocking tasks.
    • Added worker_index_block_on_multi_thread test to verify None for block_on in multi-thread runtime.
Activity
  • The pull request was created by martin-augment.
  • The pull request is awaiting review, as indicated by the description 'To review by AI'.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 3, 2026

Walkthrough

This pull request introduces a new worker_index() API function that returns the index of the current Tokio runtime worker thread, gated behind the tokio_unstable configuration flag. The implementation spans multiple modules: the public-facing API in tokio/src/runtime/mod.rs, internal delegation in tokio/src/runtime/context.rs, scheduler-level context implementation in tokio/src/runtime/scheduler/mod.rs, and multi-threaded worker-specific logic in tokio/src/runtime/scheduler/multi_thread/worker.rs. A comprehensive test suite validates behavior across multi-threaded and current-thread runtimes, as well as edge cases like blocking contexts and calls from outside a runtime.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch pr-7921-2026-03-03-12-39-33

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new tokio::runtime::worker_index() function to get the index of the current worker thread. The implementation is well-structured, correctly plumbed through the runtime and scheduler layers, and is exposed under the tokio_unstable flag. The documentation is comprehensive, and the accompanying tests are thorough. No security vulnerabilities were found. I have one minor suggestion to improve code conciseness.


#[cfg(tokio_unstable)]
pub(crate) fn worker_index() -> Option<usize> {
with_scheduler(|ctx| ctx.and_then(|c| c.worker_index()))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The closure |c| c.worker_index() can be simplified by passing the method as a function pointer directly to and_then. This is more idiomatic and concise.

Suggested change
with_scheduler(|ctx| ctx.and_then(|c| c.worker_index()))
with_scheduler(|ctx| ctx.and_then(scheduler::Context::worker_index))

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value:good-but-wont-fix; category:bug; feedback: The Gemini AI reviewer is correct! The function pointer may be used instead of the closure. But this is a matter of taste and the Tokio project already uses many closures in such use cases, so this one will stay for consistency

@augmentcode
Copy link
Copy Markdown

augmentcode bot commented Mar 3, 2026

🤖 Augment PR Summary

Summary: This PR introduces a new unstable helper for identifying which runtime worker thread is executing the current task.

Changes:

  • Adds a thread-local based implementation to retrieve the current worker index from the runtime scheduler context
  • Exposes tokio::runtime::worker_index() (under the existing unstable API gating) returning Option<usize>
  • Implements support for both current-thread (always Some(0)) and multi-thread (per-worker index) schedulers
  • Adds a dedicated test suite covering multi-thread, current-thread, outside-runtime, block_on, and spawn_blocking behavior

Technical Notes: The API is wired through runtime::contextscheduler::Context and is only available when tokio_unstable is enabled.

🤖 Was this summary useful? React with 👍 or 👎

Copy link
Copy Markdown

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. No suggestions at this time.

Comment augment review to trigger a new review at any time.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tokio/tests/rt_worker_index.rs (1)

71-94: Optional: add a block_in_place behavior test.

Given the API’s context-sensitive behavior, a dedicated block_in_place assertion would further harden regressions around runtime-entry transitions.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tokio/tests/rt_worker_index.rs` around lines 71 - 94, Add a test that asserts
runtime::worker_index reports a worker thread when code runs inside
tokio::task::block_in_place: create a new Runtime (Runtime::new()), use
rt.block_on to run an async block that calls tokio::task::block_in_place(||
runtime::worker_index()), await and unwrap the result, then assert_eq!(index,
Some(_)) (or assert!(index.is_some())) to validate block_in_place transitions to
a worker context; put this alongside the existing worker_index tests and
reference runtime::worker_index and tokio::task::block_in_place in the new test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tokio/tests/rt_worker_index.rs`:
- Around line 71-94: Add a test that asserts runtime::worker_index reports a
worker thread when code runs inside tokio::task::block_in_place: create a new
Runtime (Runtime::new()), use rt.block_on to run an async block that calls
tokio::task::block_in_place(|| runtime::worker_index()), await and unwrap the
result, then assert_eq!(index, Some(_)) (or assert!(index.is_some())) to
validate block_in_place transitions to a worker context; put this alongside the
existing worker_index tests and reference runtime::worker_index and
tokio::task::block_in_place in the new test.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b3f1a84 and f6c57e4.

📒 Files selected for processing (5)
  • tokio/src/runtime/context.rs
  • tokio/src/runtime/mod.rs
  • tokio/src/runtime/scheduler/mod.rs
  • tokio/src/runtime/scheduler/multi_thread/worker.rs
  • tokio/tests/rt_worker_index.rs

@claude
Copy link
Copy Markdown

claude bot commented Mar 3, 2026

PR Review: runtime: add worker_index() getter

Overall this is a well-motivated addition. The implementation is clean, the layering through the scheduler context is correct, and the test suite covers the important cases. A few things worth discussing before merging:


Correctness

Context::MultiThread always wraps in Some (scheduler/mod.rs:301)

The multi_thread::Context::worker_index() method unconditionally returns self.worker.index. This is only reachable when a scheduler context is actually set (guarded by with_scheduler), so a valid worker index is always present. The logic is correct, but the double-wrapping in Option (inner returns usize, outer wraps in Some) is a bit opaque. A brief comment explaining why this is always Some would help future readers.


Documentation gap: worker index is not stable across .await points

The docs describe what the function returns, but do not warn that the index can change between suspension points. In the multi-thread scheduler, a task can be stolen and resume on a different worker. Users correlating per-worker metrics with task activity need to know they should capture the index at a specific point, not assume it remains constant for the task lifetime. This is the most likely source of misuse and should be called out explicitly in the doc comment.


Test file is over-gated on target_has_atomic = "64"

The target_has_atomic = "64" flag at the top of the test file is needed for RuntimeMetrics (used in worker_index_matches_metrics_worker_thread_id), but not for the other five tests. This means worker_index_outside_runtime, worker_index_current_thread, worker_index_from_spawn_blocking, etc. are silently skipped on 32-bit targets where worker_index() should still work correctly.

Consider splitting the file or using per-test cfg attributes so the metrics-specific test carries that gate while the rest run on all supported platforms.


Missing test: LocalRuntime

LocalRuntime uses the CurrentThread scheduler, so worker_index() should return Some(0). There is no test for this. Given that LocalRuntime is a distinct public type, a test would both document and verify the behaviour.


Minor: doctest does not demonstrate the primary use case

The doc example shows the raw call but not the correlation with RuntimeMetrics that motivates the whole feature. A brief second example (even as a no_run snippet) showing the intended pattern of capturing the index and reading metrics.worker_total_busy_duration(index) would make the docs much more useful for the target audience.


Nit: cfg_unstable! vs inline cfg(tokio_unstable)

The internal helpers in context.rs, scheduler/mod.rs, and worker.rs use raw #[cfg(tokio_unstable)], consistent with other internal-only unstable code. The public function correctly goes through cfg_unstable\! which also emits the docsrs attribute. No action needed, just confirming this is intentional.


Summary

Area Status
Correctness Logic is sound
Test coverage Over-gated on 32-bit targets; no LocalRuntime test
Documentation Missing caveat that the index can change across .await points
API design Naming, return type, and None semantics are appropriate
Feature gating Correctly behind tokio_unstable throughout the stack

The stability caveat in the docs is the most important change to address before merging; everything else is incremental improvement.

@martin-augment
Copy link
Copy Markdown
Owner Author

Documentation gap: worker index is not stable across .await points

The docs describe what the function returns, but do not warn that the index can change between suspension points. In the multi-thread scheduler, a task can be stolen and resume on a different worker. Users correlating per-worker metrics with task activity need to know they should capture the index at a specific point, not assume it remains constant for the task lifetime. This is the most likely source of misuse and should be called out explicitly in the doc comment.

value:good-to-have; category:documentation; feedback: The Claude AI reviewer is correct! Await points are the place where the Async runtime may suspend a task and later resume it on a different worked thread. Since this new method is all about worker thread index it is important to mention this in its documentation.

@martin-augment
Copy link
Copy Markdown
Owner Author

Test file is over-gated on target_has_atomic = "64"

The target_has_atomic = "64" flag at the top of the test file is needed for RuntimeMetrics (used in worker_index_matches_metrics_worker_thread_id), but not for the other five tests. This means worker_index_outside_runtime, worker_index_current_thread, worker_index_from_spawn_blocking, etc. are silently skipped on 32-bit targets where worker_index() should still work correctly.

Consider splitting the file or using per-test cfg attributes so the metrics-specific test carries that gate while the rest run on all supported platforms.

value:good-to-have; category:bug; feedback: The Claude AI reviewer is correct! Most of the test cases do not need the metrics registry, so they could be executed without this requirement being fulfilled. It would be good to run more tests on more platforms to catch any bugs as early as possible.

@martin-augment
Copy link
Copy Markdown
Owner Author

Missing test: LocalRuntime

LocalRuntime uses the CurrentThread scheduler, so worker_index() should return Some(0). There is no test for this. Given that LocalRuntime is a distinct public type, a test would both document and verify the behaviour.

value:good-to-have; category:bug; feedback: The Claude AI reviewer is correct! Adding a test case for LocalRuntime will document it explicitly how this new method should behave on this kind of a runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants