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

rt: add a method to retrieve task id #5171

Merged
merged 16 commits into from Nov 13, 2022
Merged

rt: add a method to retrieve task id #5171

merged 16 commits into from Nov 13, 2022

Conversation

agayev
Copy link
Member

@agayev agayev commented Nov 5, 2022

Fixes #1996.
Closes #4721.

@github-actions github-actions bot added the R-loom Run loom tests on this PR label Nov 5, 2022
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-task Module: tokio/task labels Nov 5, 2022
@agayev
Copy link
Member Author

agayev commented Nov 6, 2022

After the first PR, I realized that I haven't put added the test that forces a panic, and after I added that test, it failed; trying to figure out why....

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

In general, this change looks very good. It is rather compact, which is nice. I have a bunch of comments related to making sure that ids are set properly during destructors and that nested tasks handle ids correctly, and I expect some of the requested tests to fail under the current code. However, I believe that the changes to fix this are relatively small.

tokio/src/runtime/context.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/harness.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/harness.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/tests/task_panic.rs Outdated Show resolved Hide resolved
tokio/tests/task_local.rs Outdated Show resolved Hide resolved
@agayev agayev force-pushed the task-id branch 2 times, most recently from 300e938 to 82cc448 Compare November 9, 2022 11:56
tokio/src/runtime/context.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/tests/task_local.rs Outdated Show resolved Hide resolved
@agayev
Copy link
Member Author

agayev commented Nov 10, 2022

@hawkw @Darksonn taking a step back and looking at it again, I think it will be cleaner if we store task_id in Stage enum, as in:

pub(super) enum Stage<T: Future> {
    Running(T, Id),
    Finished(super::Result<T::Output>, Id),
    Consumed,
}

I already suggested this to @Darksonn in discord and she dismissed it, but I didn't give my reasoning back then, so let me give it now.

Only three functions need to set the task id in the context:

  • CoreStage::poll -- for making id available during the normal execution of the task
  • CoreStage::drop_future_or_output -- for making id available when dropping a future or output
  • CoreStage::store_output -- for making id available during the dropping of future

If we store id in the Stage enum, we can keep id setting stuff completely within core.rs file in just those functions and not leak it outside, and we can be sure that all future uses of these functions and moving them around will be covered. Otherwise, what we have currently is not reliable because we should find places where one of those functions are called and put TaskIdGuard around it.

The alternative is to change those three functions to accept the id argument and then have them enter the guards. I've partially done this for store_output in this PR, but it is incomplete/inconsistent at the moment: I set TaskGuardId in poll_future, which covers both, the call to core.poll(cx) and the call to store_output. So when the future destructor runs during the call to store_output, it can access the id. I've extended Stage enum to store Id with Finished variant, so store_output saves the Id there, so that when drop_future_output drops the output there, the output destructor can also access the id. As you can tell, this is messy and inconsistent.

Which one do you prefer, or do you have a better suggestion?

@Darksonn
Copy link
Contributor

Darksonn commented Nov 10, 2022

Thank you, that motivation seems reasonable enough. However, there's also another option: move the CoreStage methods to Core instead, so that they can access the id without changing the struct layout. What do you think?

I would prefer a solution where the id is not deleted from the task when the output is consumed so that the id field can be removed from JoinHandle in the future:

pub struct JoinHandle<T> {
    raw: Option<RawTask>,
    id: Id,
    _p: PhantomData<T>,
}

@agayev
Copy link
Member Author

agayev commented Nov 10, 2022

Thank you, that motivation seems reasonable enough. However, there's also another option: move the CoreStage methods to Core instead, so that they can access the id without changing the struct layout. What do you think?

I would prefer a solution where the id is not deleted from the task when the output is consumed so that the id field can be removed from JoinHandle in the future:

pub struct JoinHandle<T> {
    raw: Option<RawTask>,
    id: Id,
    _p: PhantomData<T>,
}

Sure, that also works. See #5182.

@agayev
Copy link
Member Author

agayev commented Nov 10, 2022

@Darksonn @hawkw rebased and rewrote now that CoreStage functions are in core.

Two points:

  • I think I was wrong in my original assessment that store_output also needs guards -- from my cursory reading it appears that by the time store_output is called, the future in the stage is already destroyed by drop_future_or_output.
  • Haven't implemented the following test yet from @Darksonn 's first list "If you use block_in_place + spawn + block_on on a current-thread runtime to execute a spawned task within another task, the original task's id is not erased." I'll bug @Darksonn on discord to get more clarity on that test

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

I haven't looked over the tests yet, but in general this looks quite nice and clean. 👍

tokio/src/runtime/context.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/core.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Show resolved Hide resolved
tokio/src/task/mod.rs Outdated Show resolved Hide resolved
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

overall, the implementation looks good to me! most of my remaining suggestions concern the documentation and tests added in this PR.

tokio/src/runtime/task/core.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Show resolved Hide resolved
tokio/tests/task_local.rs Outdated Show resolved Hide resolved
@hawkw
Copy link
Member

hawkw commented Nov 10, 2022

it looks like there are currently some test failures when building with --no-default-features: https://github.com/tokio-rs/tokio/actions/runs/3439239014/jobs/5736297357#step:9:14

fix clippy warnings

fix clippy warnings and address the issues raised by @Darksonn

fix more clippy warnings

Update tokio/src/runtime/task/core.rs

Co-authored-by: Eliza Weisman <eliza@buoyant.io>

Update tokio/src/runtime/task/mod.rs

Co-authored-by: Eliza Weisman <eliza@buoyant.io>

fix nits raised by @hawkw
@agayev
Copy link
Member Author

agayev commented Nov 10, 2022

@hawkw addressed all the issues you raised. I experimented with github's web interface for committing for the first time and messed up some things, so had to squash, rebase, and force-push -- apologies @Darksonn.

I can't reproduce the build failure even if I try --not-default-features... Let's see if it happens again.

@agayev
Copy link
Member Author

agayev commented Nov 11, 2022

@Darksonn @hawkw passed all the tests -- ready for review!

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Panics on spawned tasks do not ensure that the test fails. You have to ensure that the test fails if the panic happens. I commented on two of them, but it applies to many more tests.

tokio/src/runtime/context.rs Outdated Show resolved Hide resolved
tokio/src/runtime/task/mod.rs Outdated Show resolved Hide resolved
tokio/tests/task_id.rs Outdated Show resolved Hide resolved
tokio/tests/task_id.rs Outdated Show resolved Hide resolved
tokio/tests/task_id.rs Outdated Show resolved Hide resolved
tokio/tests/task_id.rs Outdated Show resolved Hide resolved
tokio/tests/task_id.rs Outdated Show resolved Hide resolved
tokio/tests/task_id.rs Outdated Show resolved Hide resolved
tokio/tests/task_id.rs Outdated Show resolved Hide resolved
tokio/tests/task_id.rs Show resolved Hide resolved
tokio/tests/task_id.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@Darksonn Darksonn merged commit 71bd49e into tokio-rs:master Nov 13, 2022
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this pull request Nov 22, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [tokio](https://tokio.rs) ([source](https://github.com/tokio-rs/tokio)) | dependencies | minor | `1.21.2` -> `1.22.0` |
| [tokio](https://tokio.rs) ([source](https://github.com/tokio-rs/tokio)) | dev-dependencies | minor | `1.21.2` -> `1.22.0` |

---

### Release Notes

<details>
<summary>tokio-rs/tokio</summary>

### [`v1.22.0`](https://github.com/tokio-rs/tokio/releases/tag/tokio-1.22.0): Tokio v1.22.0

[Compare Source](tokio-rs/tokio@tokio-1.21.2...tokio-1.22.0)

##### Added

-   runtime: add `Handle::runtime_flavor` ([#&#8203;5138])
-   sync: add `Mutex::blocking_lock_owned` ([#&#8203;5130])
-   sync: add `Semaphore::MAX_PERMITS` ([#&#8203;5144])
-   sync: add `merge()` to semaphore permits ([#&#8203;4948])
-   sync: add `mpsc::WeakUnboundedSender` ([#&#8203;5189])

##### Added (unstable)

-   process: add `Command::process_group` ([#&#8203;5114])
-   runtime: export metrics about the blocking thread pool ([#&#8203;5161])
-   task: add `task::id()` and `task::try_id()` ([#&#8203;5171])

##### Fixed

-   macros: don't take ownership of futures in macros ([#&#8203;5087])
-   runtime: fix Stacked Borrows violation in `LocalOwnedTasks` ([#&#8203;5099])
-   runtime: mitigate ABA with 32-bit queue indices when possible ([#&#8203;5042])
-   task: wake local tasks to the local queue when woken by the same thread ([#&#8203;5095])
-   time: panic in release mode when `mark_pending` called illegally ([#&#8203;5093])
-   runtime: fix typo in expect message ([#&#8203;5169])
-   runtime: fix `unsync_load` on atomic types ([#&#8203;5175])
-   task: elaborate safety comments in task deallocation ([#&#8203;5172])
-   runtime: fix `LocalSet` drop in thread local ([#&#8203;5179])
-   net: remove libc type leakage in a public API ([#&#8203;5191])
-   runtime: update the alignment of `CachePadded` ([#&#8203;5106])

##### Changed

-   io: make `tokio::io::copy` continue filling the buffer when writer stalls ([#&#8203;5066])
-   runtime: remove `coop::budget` from `LocalSet::run_until` ([#&#8203;5155])
-   sync: make `Notify` panic safe ([#&#8203;5154])

##### Documented

-   io: fix doc for `write_i8` to use signed integers ([#&#8203;5040])
-   net: fix doc typos for TCP and UDP `set_tos` methods ([#&#8203;5073])
-   net: fix function name in `UdpSocket::recv` documentation ([#&#8203;5150])
-   sync: typo in `TryLockError` for `RwLock::try_write` ([#&#8203;5160])
-   task: document that spawned tasks execute immediately ([#&#8203;5117])
-   time: document return type of `timeout` ([#&#8203;5118])
-   time: document that `timeout` checks only before poll ([#&#8203;5126])
-   sync: specify return type of `oneshot::Receiver` in docs ([#&#8203;5198])

##### Internal changes

-   runtime: use const `Mutex::new` for globals ([#&#8203;5061])
-   runtime: remove `Option` around `mio::Events` in io driver ([#&#8203;5078])
-   runtime: remove a conditional compilation clause ([#&#8203;5104])
-   runtime: remove a reference to internal time handle ([#&#8203;5107])
-   runtime: misc time driver cleanup ([#&#8203;5120])
-   runtime: move signal driver to runtime module ([#&#8203;5121])
-   runtime: signal driver now uses I/O driver directly ([#&#8203;5125])
-   runtime: start decoupling I/O driver and I/O handle ([#&#8203;5127])
-   runtime: switch `io::handle` refs with scheduler:Handle ([#&#8203;5128])
-   runtime: remove Arc from I/O driver ([#&#8203;5134])
-   runtime: use signal driver handle via `scheduler::Handle` ([#&#8203;5135])
-   runtime: move internal clock fns out of context ([#&#8203;5139])
-   runtime: remove `runtime::context` module ([#&#8203;5140])
-   runtime: keep driver cfgs in `driver.rs` ([#&#8203;5141])
-   runtime: add `runtime::context` to unify thread-locals ([#&#8203;5143])
-   runtime: rename some confusing internal variables/fns ([#&#8203;5151])
-   runtime: move `coop` mod into `runtime` ([#&#8203;5152])
-   runtime: move budget state to context thread-local ([#&#8203;5157])
-   runtime: move park logic into runtime module ([#&#8203;5158])
-   runtime: move `Runtime` into its own file ([#&#8203;5159])
-   runtime: unify entering a runtime with `Handle::enter` ([#&#8203;5163])
-   runtime: remove handle reference from each scheduler ([#&#8203;5166])
-   runtime: move `enter` into `context` ([#&#8203;5167])
-   runtime: combine context and entered thread-locals ([#&#8203;5168])
-   runtime: fix accidental unsetting of current handle ([#&#8203;5178])
-   runtime: move `CoreStage` methods to `Core` ([#&#8203;5182])
-   sync: name mpsc semaphore types ([#&#8203;5146])

[#&#8203;4948]: tokio-rs/tokio#4948

[#&#8203;5040]: tokio-rs/tokio#5040

[#&#8203;5042]: tokio-rs/tokio#5042

[#&#8203;5061]: tokio-rs/tokio#5061

[#&#8203;5066]: tokio-rs/tokio#5066

[#&#8203;5073]: tokio-rs/tokio#5073

[#&#8203;5078]: tokio-rs/tokio#5078

[#&#8203;5087]: tokio-rs/tokio#5087

[#&#8203;5093]: tokio-rs/tokio#5093

[#&#8203;5095]: tokio-rs/tokio#5095

[#&#8203;5099]: tokio-rs/tokio#5099

[#&#8203;5104]: tokio-rs/tokio#5104

[#&#8203;5106]: tokio-rs/tokio#5106

[#&#8203;5107]: tokio-rs/tokio#5107

[#&#8203;5114]: tokio-rs/tokio#5114

[#&#8203;5117]: tokio-rs/tokio#5117

[#&#8203;5118]: tokio-rs/tokio#5118

[#&#8203;5120]: tokio-rs/tokio#5120

[#&#8203;5121]: tokio-rs/tokio#5121

[#&#8203;5125]: tokio-rs/tokio#5125

[#&#8203;5126]: tokio-rs/tokio#5126

[#&#8203;5127]: tokio-rs/tokio#5127

[#&#8203;5128]: tokio-rs/tokio#5128

[#&#8203;5130]: tokio-rs/tokio#5130

[#&#8203;5134]: tokio-rs/tokio#5134

[#&#8203;5135]: tokio-rs/tokio#5135

[#&#8203;5138]: tokio-rs/tokio#5138

[#&#8203;5138]: tokio-rs/tokio#5138

[#&#8203;5139]: tokio-rs/tokio#5139

[#&#8203;5140]: tokio-rs/tokio#5140

[#&#8203;5141]: tokio-rs/tokio#5141

[#&#8203;5143]: tokio-rs/tokio#5143

[#&#8203;5144]: tokio-rs/tokio#5144

[#&#8203;5144]: tokio-rs/tokio#5144

[#&#8203;5146]: tokio-rs/tokio#5146

[#&#8203;5150]: tokio-rs/tokio#5150

[#&#8203;5151]: tokio-rs/tokio#5151

[#&#8203;5152]: tokio-rs/tokio#5152

[#&#8203;5154]: tokio-rs/tokio#5154

[#&#8203;5155]: tokio-rs/tokio#5155

[#&#8203;5157]: tokio-rs/tokio#5157

[#&#8203;5158]: tokio-rs/tokio#5158

[#&#8203;5159]: tokio-rs/tokio#5159

[#&#8203;5160]: tokio-rs/tokio#5160

[#&#8203;5161]: tokio-rs/tokio#5161

[#&#8203;5163]: tokio-rs/tokio#5163

[#&#8203;5166]: tokio-rs/tokio#5166

[#&#8203;5167]: tokio-rs/tokio#5167

[#&#8203;5168]: tokio-rs/tokio#5168

[#&#8203;5169]: tokio-rs/tokio#5169

[#&#8203;5171]: tokio-rs/tokio#5171

[#&#8203;5172]: tokio-rs/tokio#5172

[#&#8203;5175]: tokio-rs/tokio#5175

[#&#8203;5178]: tokio-rs/tokio#5178

[#&#8203;5179]: tokio-rs/tokio#5179

[#&#8203;5182]: tokio-rs/tokio#5182

[#&#8203;5189]: tokio-rs/tokio#5189

[#&#8203;5191]: tokio-rs/tokio#5191

[#&#8203;5198]: tokio-rs/tokio#5198

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4yNy4xIiwidXBkYXRlZEluVmVyIjoiMzQuMjkuMiJ9-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1651
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
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 M-task Module: tokio/task R-loom Run loom tests on this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

async fn and async block not support task::current().id(),how to get Coroutines id?
3 participants