From bc62e2d94cb84d556af7d6a707405baa1058e832 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Sat, 18 Apr 2026 10:11:14 -0700 Subject: [PATCH] rt: don't notify when pushing task to empty LIFO slot See #8065 --- .../runtime/scheduler/multi_thread/worker.rs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tokio/src/runtime/scheduler/multi_thread/worker.rs b/tokio/src/runtime/scheduler/multi_thread/worker.rs index ab3b38c3f..42a52837f 100644 --- a/tokio/src/runtime/scheduler/multi_thread/worker.rs +++ b/tokio/src/runtime/scheduler/multi_thread/worker.rs @@ -1349,9 +1349,12 @@ impl Handle { // task must always be pushed to the back of the queue, enabling other // tasks to be executed. If **not** a yield, then there is more // flexibility and the task may go to the front of the queue. - if is_yield || !core.lifo_enabled { + let should_notify = if is_yield || !core.lifo_enabled { core.run_queue .push_back_or_overflow(task, self, &mut core.stats); + // Always notify another parked worker when pushing a yielded task + // to the main run queue if we are not currently parked. + true } else { // Push to the LIFO slot if let Some(prev) = core.run_queue.push_lifo(task) { @@ -1359,13 +1362,26 @@ impl Handle { // to be pushed to the back of the run queue. core.run_queue .push_back_or_overflow(prev, self, &mut core.stats); + // We have pushed the previous LIFO task to the back of the run + // queue, so we should attempt to notify another worker if we + // are not currently parked. + true + } else { + // If the scheduled task was pushed to the LIFO slot and there + // is no other task previously in the slot, skip notifying + // another worker, so that we can preferentially poll the LIFO + // task next. It can still be stolen if another worker is + // searching. This reduces cross thread notifications, and + // reduces the chance of the LIFO task moving across threads if + // the next poll is short. + false } }; // Only notify if not currently parked. If `park` is `None`, then the // scheduling is from a resource driver. As notifications often come in // batches, the notification is delayed until the park is complete. - if core.park.is_some() { + if should_notify && core.park.is_some() { self.notify_parked_local(); } }