From 2521182bd117f73e647b18126a7fcc98ff440390 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 15 Apr 2026 08:37:08 +0200 Subject: [PATCH] Adjust spawn_blocking() queue resize check --- tokio/src/runtime/blocking/sharded_queue.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/tokio/src/runtime/blocking/sharded_queue.rs b/tokio/src/runtime/blocking/sharded_queue.rs index 2fcc925c458..70f4bdf90d6 100644 --- a/tokio/src/runtime/blocking/sharded_queue.rs +++ b/tokio/src/runtime/blocking/sharded_queue.rs @@ -61,18 +61,11 @@ impl Shard { let mut new_queue = VecDeque::with_capacity(new_cap); queue = self.queue.lock(); - // If the queue is: - // a) Not full anymore => push to the current queue - // b) Full and our new queue is big enough => copy items to the new - // queue and push to it. - // c) Full and our new queue is too small => try again. - if queue.len() == queue.capacity() { - if new_queue.capacity() > queue.len() { - new_queue.extend(queue.drain(..)); - *queue = new_queue; - break; - } - } else { + // We went to the effort to allocate this memory, so use it to increase the capacity + // even if the queue now has capacity. + if new_queue.capacity() > queue.capacity() { + new_queue.extend(queue.drain(..)); + *queue = new_queue; break; } }