-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
task: wake local tasks to the local queue when woken by the same thread #5095
Conversation
uhhh...looks like the assertion is failing on freebsd (https://cirrus-ci.com/task/5075280174776320?logs=test#L1661)? that's very surprising given that |
## Motivation It turns out that `LocalSet` has...never actually passed Miri. This is because all of the tests for `LocalSet` are defined as integration tests in `tokio/tests/task_local_set.rs`, rather than lib tests in `tokio/src`, and we never run Miri for integration tests. PR #5095 added a new test reproducing an unrelated bug in `LocalSet`, which had to be implemented as a lib test, as it must make internal assertions about the state of the `LocalSet` that cannot be tested with just the public API. This test failed under Miri, and it turns out that the reason for this is unrelated to the change in #5095 --- `LocalSet` uses the `LocalOwnedTasks` type, which turns out to contain a stacked borrows violation due to converting an `&Header` into an `NonNull<Header>` when removing a task from the `LocalOwnedTasks` list. ## Solution Fortunately, this was actually quite easy to fix. The non-`Local` version of `OwnedTasks` already uses `Task::header_ptr()` to get a `NonNull<Header>` in its version of `remove`, which avoids this issue. Therefore, the fix was as simple as updating `LocalOwnedTasks` to do the same. I've also added a very simple lib test in `src/task/local_set.rs` that just creates a `LocalSet` and spawns a single task on it under a current-thread runtime. This test fails under Miri without the fix in this PR: https://github.com/tokio-rs/tokio/actions/runs/3237072694/jobs/5303682530 We should probably keep the test so that we're always testing at least the most trivial `LocalSet` usage under Miri...
## Motivation It turns out that `LocalSet` has...never actually passed Miri. This is because all of the tests for `LocalSet` are defined as integration tests in `tokio/tests/task_local_set.rs`, rather than lib tests in `tokio/src`, and we never run Miri for integration tests. PR #5095 added a new test reproducing an unrelated bug in `LocalSet`, which had to be implemented as a lib test, as it must make internal assertions about the state of the `LocalSet` that cannot be tested with just the public API. This test failed under Miri, and it turns out that the reason for this is unrelated to the change in #5095 --- `LocalSet` uses the `LocalOwnedTasks` type, which turns out to contain a stacked borrows violation due to converting an `&Header` into an `NonNull<Header>` when removing a task from the `LocalOwnedTasks` list. ## Solution Fortunately, this was actually quite easy to fix. The non-`Local` version of `OwnedTasks` already uses `Task::header_ptr()` to get a `NonNull<Header>` in its version of `remove`, which avoids this issue. Therefore, the fix was as simple as updating `LocalOwnedTasks` to do the same. I've also added a very simple lib test in `src/task/local_set.rs` that just creates a `LocalSet` and spawns a single task on it under a current-thread runtime. This test fails under Miri without the fix in this PR: https://github.com/tokio-rs/tokio/actions/runs/3237072694/jobs/5303682530 We should probably keep the test so that we're always testing at least the most trivial `LocalSet` usage under Miri...
this also handles the annoying issue that the thread id's value is not guaranteed to be correct while dropping a thread-local...
i forgot to import `Thread`/`ThreadId` from `loom` to use the mocked thread IDs during loom runs. this should debreak the loom tests.
a434387
to
1aa7a39
Compare
This reverts commit 61b29c6.
This reverts commit 6ce9caa.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks.
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` ([#​5138]) - sync: add `Mutex::blocking_lock_owned` ([#​5130]) - sync: add `Semaphore::MAX_PERMITS` ([#​5144]) - sync: add `merge()` to semaphore permits ([#​4948]) - sync: add `mpsc::WeakUnboundedSender` ([#​5189]) ##### Added (unstable) - process: add `Command::process_group` ([#​5114]) - runtime: export metrics about the blocking thread pool ([#​5161]) - task: add `task::id()` and `task::try_id()` ([#​5171]) ##### Fixed - macros: don't take ownership of futures in macros ([#​5087]) - runtime: fix Stacked Borrows violation in `LocalOwnedTasks` ([#​5099]) - runtime: mitigate ABA with 32-bit queue indices when possible ([#​5042]) - task: wake local tasks to the local queue when woken by the same thread ([#​5095]) - time: panic in release mode when `mark_pending` called illegally ([#​5093]) - runtime: fix typo in expect message ([#​5169]) - runtime: fix `unsync_load` on atomic types ([#​5175]) - task: elaborate safety comments in task deallocation ([#​5172]) - runtime: fix `LocalSet` drop in thread local ([#​5179]) - net: remove libc type leakage in a public API ([#​5191]) - runtime: update the alignment of `CachePadded` ([#​5106]) ##### Changed - io: make `tokio::io::copy` continue filling the buffer when writer stalls ([#​5066]) - runtime: remove `coop::budget` from `LocalSet::run_until` ([#​5155]) - sync: make `Notify` panic safe ([#​5154]) ##### Documented - io: fix doc for `write_i8` to use signed integers ([#​5040]) - net: fix doc typos for TCP and UDP `set_tos` methods ([#​5073]) - net: fix function name in `UdpSocket::recv` documentation ([#​5150]) - sync: typo in `TryLockError` for `RwLock::try_write` ([#​5160]) - task: document that spawned tasks execute immediately ([#​5117]) - time: document return type of `timeout` ([#​5118]) - time: document that `timeout` checks only before poll ([#​5126]) - sync: specify return type of `oneshot::Receiver` in docs ([#​5198]) ##### Internal changes - runtime: use const `Mutex::new` for globals ([#​5061]) - runtime: remove `Option` around `mio::Events` in io driver ([#​5078]) - runtime: remove a conditional compilation clause ([#​5104]) - runtime: remove a reference to internal time handle ([#​5107]) - runtime: misc time driver cleanup ([#​5120]) - runtime: move signal driver to runtime module ([#​5121]) - runtime: signal driver now uses I/O driver directly ([#​5125]) - runtime: start decoupling I/O driver and I/O handle ([#​5127]) - runtime: switch `io::handle` refs with scheduler:Handle ([#​5128]) - runtime: remove Arc from I/O driver ([#​5134]) - runtime: use signal driver handle via `scheduler::Handle` ([#​5135]) - runtime: move internal clock fns out of context ([#​5139]) - runtime: remove `runtime::context` module ([#​5140]) - runtime: keep driver cfgs in `driver.rs` ([#​5141]) - runtime: add `runtime::context` to unify thread-locals ([#​5143]) - runtime: rename some confusing internal variables/fns ([#​5151]) - runtime: move `coop` mod into `runtime` ([#​5152]) - runtime: move budget state to context thread-local ([#​5157]) - runtime: move park logic into runtime module ([#​5158]) - runtime: move `Runtime` into its own file ([#​5159]) - runtime: unify entering a runtime with `Handle::enter` ([#​5163]) - runtime: remove handle reference from each scheduler ([#​5166]) - runtime: move `enter` into `context` ([#​5167]) - runtime: combine context and entered thread-locals ([#​5168]) - runtime: fix accidental unsetting of current handle ([#​5178]) - runtime: move `CoreStage` methods to `Core` ([#​5182]) - sync: name mpsc semaphore types ([#​5146]) [#​4948]: tokio-rs/tokio#4948 [#​5040]: tokio-rs/tokio#5040 [#​5042]: tokio-rs/tokio#5042 [#​5061]: tokio-rs/tokio#5061 [#​5066]: tokio-rs/tokio#5066 [#​5073]: tokio-rs/tokio#5073 [#​5078]: tokio-rs/tokio#5078 [#​5087]: tokio-rs/tokio#5087 [#​5093]: tokio-rs/tokio#5093 [#​5095]: tokio-rs/tokio#5095 [#​5099]: tokio-rs/tokio#5099 [#​5104]: tokio-rs/tokio#5104 [#​5106]: tokio-rs/tokio#5106 [#​5107]: tokio-rs/tokio#5107 [#​5114]: tokio-rs/tokio#5114 [#​5117]: tokio-rs/tokio#5117 [#​5118]: tokio-rs/tokio#5118 [#​5120]: tokio-rs/tokio#5120 [#​5121]: tokio-rs/tokio#5121 [#​5125]: tokio-rs/tokio#5125 [#​5126]: tokio-rs/tokio#5126 [#​5127]: tokio-rs/tokio#5127 [#​5128]: tokio-rs/tokio#5128 [#​5130]: tokio-rs/tokio#5130 [#​5134]: tokio-rs/tokio#5134 [#​5135]: tokio-rs/tokio#5135 [#​5138]: tokio-rs/tokio#5138 [#​5138]: tokio-rs/tokio#5138 [#​5139]: tokio-rs/tokio#5139 [#​5140]: tokio-rs/tokio#5140 [#​5141]: tokio-rs/tokio#5141 [#​5143]: tokio-rs/tokio#5143 [#​5144]: tokio-rs/tokio#5144 [#​5144]: tokio-rs/tokio#5144 [#​5146]: tokio-rs/tokio#5146 [#​5150]: tokio-rs/tokio#5150 [#​5151]: tokio-rs/tokio#5151 [#​5152]: tokio-rs/tokio#5152 [#​5154]: tokio-rs/tokio#5154 [#​5155]: tokio-rs/tokio#5155 [#​5157]: tokio-rs/tokio#5157 [#​5158]: tokio-rs/tokio#5158 [#​5159]: tokio-rs/tokio#5159 [#​5160]: tokio-rs/tokio#5160 [#​5161]: tokio-rs/tokio#5161 [#​5163]: tokio-rs/tokio#5163 [#​5166]: tokio-rs/tokio#5166 [#​5167]: tokio-rs/tokio#5167 [#​5168]: tokio-rs/tokio#5168 [#​5169]: tokio-rs/tokio#5169 [#​5171]: tokio-rs/tokio#5171 [#​5172]: tokio-rs/tokio#5172 [#​5175]: tokio-rs/tokio#5175 [#​5178]: tokio-rs/tokio#5178 [#​5179]: tokio-rs/tokio#5179 [#​5182]: tokio-rs/tokio#5182 [#​5189]: tokio-rs/tokio#5189 [#​5191]: tokio-rs/tokio#5191 [#​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 <[email protected]> Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1651 Reviewed-by: crapStone <[email protected]> Co-authored-by: Calciumdibromid Bot <[email protected]> Co-committed-by: Calciumdibromid Bot <[email protected]>
Motivation
Currently, when a task spawned on a
LocalSet
is woken by an I/O driveror time driver running on the same thread as the
LocalSet
, the task ispushed to the
LocalSet
's locked remote run queue, rather than to itsunsynchronized local run queue. This is somewhat unfortunate, as it
negates some of the performance benefit of having an unsynchronized
local run queue. Instead, tasks are only woken to the local queue when
they are woken by other tasks also running on the local set.
This occurs because the local queue is only used when the
CONTEXT
thread-local contains a
Context
that's the same as the task'sSchedule
instance (anArc<Shared>
)'sContext
. When theLocalSet
is not being polled, the thread-local is unset, and the local run queue
cannot be accessed by the
Schedule
implementation forArc<Shared>
.Solution
This branch fixes this by moving the local run queue into
Shared
alongwith the remote run queue. When an
Arc<Shared>
'sSchedule
impl wakesa task and the
CONTEXT
thread-local isNone
(indicating we are notcurrently polling the
LocalSet
on this thread), we now check if thecurrent thread's
ThreadId
matches that of the thread theLocalSet
was created on, and push the woken task to the local queue if it was.
Moving the local run queue into
Shared
is somewhat unfortunate, as itmeans we now have a single field on the
Shared
type which must not beaccessed from other threads, and must add an
unsafe impl Sync for Shared
.owever, it's the only viable way to wake to the local queue
from the
Schedule
impl forArc<Shared>
, so I figured it was worththe additional unsafe code. I added a debug assertion to check that the
local queue is only accessed from the thread that owns the
LocalSet
.