Skip to content

Conversation

@hawkw
Copy link
Member

@hawkw hawkw commented Jun 24, 2025

Motivation

As described in issue #7411, task spawning APIs are currently annotated
with #[track_caller], allowing us to capture the location in the user
source code where the task was spawned. This is used for tracing
events used by tokio-console and friends. However, this information is
not exposed to the runtime on_task_spawn, on_before_task_poll,
on_after_task_poll, and on_task_terminate hooks, which is a shame,
as it would be useful there as well.

Solution

This branch adds the task's spawn location to the TaskMeta struct
provided to the runtime's task hooks. This is implemented by storing a
&'static Location<'static> in the task's Core alongside the
task::Id. In [this comment][1], @ADD-SP suggested storing the
Location in the task's Trailer.

I opted to store it in the Core instead, as the Trailer is intended
to store "cold" data that is only accessed when the task completes,
and not on every poll. Since the task meta is passed to the
on_before_task_poll and on_after_task_poll hooks, we would be
accessing the Trailer on polls if we stored the Location there.
Therefore, I put it in the Core, instead, which contains data that we
access every time the task is polled.

Closes #7411

As described in issue #7411, task spawning APIs are currently annotated
with `#[track_caller]`, allowing us to capture the location in the user
source code where the task was spawned. This is used for `tracing`
events used by `tokio-console` and friends. However, this information is
*not* exposed to the runtime `on_task_spawn`, `on_before_task_poll`,
`on_after_task_poll`, and `on_task_terminate` hooks, which is a shame,
as it would be useful there as well.

This branch adds the task's spawn location to the `TaskMeta` struct
provided to the runtime's task hooks. This is implemented by storing a
`&'static Location<'static>` in the task's `Core` alongside the
`task::Id`. In [this comment][1], @ADD-SP suggested storing the
`Location` in the task's `Trailer`.

I opted to store it in the `Core` instead, as the `Trailer` is intended
to store "cold" data that is only accessed when the task _completes_,
and not on every poll. Since the task meta is passed to the
`on_before_task_poll` and `on_after_task_poll` hooks, we would be
accessing the `Trailer` on polls if we stored the `Location` there.
Therefore, I put it in the `Core`, instead, which contains data that we
access every time the task is polled.

Closes #7411

[1]: #7411 (comment)
@github-actions github-actions bot added R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR R-loom-multi-thread-alt labels Jun 24, 2025
@hawkw hawkw added A-tokio Area: The main tokio crate and removed R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR R-loom-multi-thread-alt labels Jun 24, 2025
@github-actions github-actions bot added R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR R-loom-multi-thread-alt labels Jun 24, 2025
@hawkw hawkw requested a review from ADD-SP June 28, 2025 18:10
Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

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

Overall looks good to me, I just left two comments, which can be addressed in follow-up PRs.

@ADD-SP ADD-SP added the C-enhancement Category: A PR with an enhancement or bugfix. label Jun 29, 2025
@hawkw
Copy link
Member Author

hawkw commented Jun 30, 2025

As a follow-up, it occurs to me that it might also be useful to add tasks' spawn locations to the trace collected by the task dump code, but I'd prefer that be done in a separate branch.

@hawkw hawkw enabled auto-merge (squash) June 30, 2025 18:10
@hawkw hawkw merged commit 3e890cc into master Jun 30, 2025
93 checks passed
@hawkw hawkw deleted the eliza/task-hooks-spawn-location branch June 30, 2025 18:13
hawkw added a commit that referenced this pull request Jul 1, 2025
# 1.46.0 (July 1st, 2025)

### Fixed

- net: fixed `TcpStream::shutdown` incorrectly returning an error on macOS
  ([#7290])

## Added

- sync: `mpsc::OwnedPermit::{same_channel, same_channel_as_sender}` methods
  ([#7389])
- macros: `biased` option for `join!` and `try_join!`, similar to `select!`
  ([#7307])
- net: support for cygwin ([#7393])
- net: support `pope::OpenOptions::read_write` on Android ([#7426])
- net: add `Clone` implementation for `net::unix::SocketAddr` ([#7422])

## Changed

- runtime: eliminate unnecessary lfence while operating on `queue::Local<T>`
  ([#7340])
- task: disallow blocking in `LocalSet::{poll,drop}` ([#7372])

## Unstable

- runtime: add `TaskMeta::spawn_location` tracking where a task was spawned
  ([#7417])
- runtime: removed borrow from `LocalOptions` parameter to
  `runtime::Builder::build_local` ([#7346])

## Documented

- io: clarify behavior of seeking when `start_seek` is not used ([#7366])
- io: document cancellation safety of `AsyncWriteExt::flush` ([#7364])
- net: fix docs for `recv_buffer_size` method ([#7336])
- net: fix broken link of `RawFd` in `TcpSocket` docs ([#7416])
- net: update `AsRawFd` doc link to current Rust stdlib location ([#7429])
- readme: fix double period in reactor description (#7363)
- runtime: add doc note that `on_*_task_poll` is unstable ([#7311])
- sync: update broadcast docs on allocation failure ([#7352])
- time: add a missing panic scenario of `time::advance` ([#7394])

[#7290]: #7290
[#7307]: #7307
[#7311]: #7311
[#7336]: #7336
[#7340]: #7340
[#7346]: #7346
[#7352]: #7352
[#7364]: #7364
[#7366]: #7366
[#7372]: #7372
[#7389]: #7389
[#7393]: #7393
[#7394]: #7394
[#7416]: #7416
[#7422]: #7422
[#7426]: #7426
[#7429]: #7429
[#7417]: #7417
hawkw added a commit that referenced this pull request Jul 1, 2025
# 1.46.0 (July 1st, 2025)

### Fixed

- net: fixed `TcpStream::shutdown` incorrectly returning an error on macOS
  ([#7290])

## Added

- sync: `mpsc::OwnedPermit::{same_channel, same_channel_as_sender}` methods
  ([#7389])
- macros: `biased` option for `join!` and `try_join!`, similar to `select!`
  ([#7307])
- net: support for cygwin ([#7393])
- net: support `pope::OpenOptions::read_write` on Android ([#7426])
- net: add `Clone` implementation for `net::unix::SocketAddr` ([#7422])

## Changed

- runtime: eliminate unnecessary lfence while operating on `queue::Local<T>`
  ([#7340])
- task: disallow blocking in `LocalSet::{poll,drop}` ([#7372])

## Unstable

- runtime: add `TaskMeta::spawn_location` tracking where a task was spawned
  ([#7417])
- runtime: removed borrow from `LocalOptions` parameter to
  `runtime::Builder::build_local` ([#7346])

## Documented

- io: clarify behavior of seeking when `start_seek` is not used ([#7366])
- io: document cancellation safety of `AsyncWriteExt::flush` ([#7364])
- net: fix docs for `recv_buffer_size` method ([#7336])
- net: fix broken link of `RawFd` in `TcpSocket` docs ([#7416])
- net: update `AsRawFd` doc link to current Rust stdlib location ([#7429])
- readme: fix double period in reactor description (#7363)
- runtime: add doc note that `on_*_task_poll` is unstable ([#7311])
- sync: update broadcast docs on allocation failure ([#7352])
- time: add a missing panic scenario of `time::advance` ([#7394])

[#7290]: #7290
[#7307]: #7307
[#7311]: #7311
[#7336]: #7336
[#7340]: #7340
[#7346]: #7346
[#7352]: #7352
[#7364]: #7364
[#7366]: #7366
[#7372]: #7372
[#7389]: #7389
[#7393]: #7393
[#7394]: #7394
[#7416]: #7416
[#7422]: #7422
[#7426]: #7426
[#7429]: #7429
[#7417]: #7417
hawkw added a commit to oxidecomputer/tokio-dtrace that referenced this pull request Jul 4, 2025
Tokio PRs tokio-rs/tokio#7417 and tokio-rs/tokio#7440 add the source
code locations at which a task was spawned to the `TaskMeta` structure
provided to the runtime task hook callbacks. These will give us the
ability to associate task IDs (which identify a task at runtime) with a
source code location in the instrumented program, which is going to make
everything a lot easier. This PR picks up those changes and updates the
minimum Tokio dependency to 1.46.1.

Closes #1
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-enhancement Category: A PR with an enhancement or bugfix. R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

runtime hooks: expose spawn caller location in on_task_spawn hook

4 participants