Skip to content

Commit e148244

Browse files
committed
Revert "Remove implicit clear in ReadyToRunQueue::drop (#2493)"
This reverts commit 37dfb05.
1 parent 5073eb9 commit e148244

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

futures-util/src/stream/futures_unordered/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl<Fut> FuturesUnordered<Fut> {
558558
pub fn clear(&mut self) {
559559
self.clear_head_all();
560560

561-
// SAFETY: we just cleared all the tasks and we have &mut self
561+
// we just cleared all the tasks, and we have &mut self, so this is safe.
562562
unsafe { self.ready_to_run_queue.clear() };
563563

564564
self.is_terminated.store(false, Relaxed);
@@ -575,9 +575,24 @@ impl<Fut> FuturesUnordered<Fut> {
575575

576576
impl<Fut> Drop for FuturesUnordered<Fut> {
577577
fn drop(&mut self) {
578+
// When a `FuturesUnordered` is dropped we want to drop all futures
579+
// associated with it. At the same time though there may be tons of
580+
// wakers flying around which contain `Task<Fut>` references
581+
// inside them. We'll let those naturally get deallocated.
578582
self.clear_head_all();
579-
// SAFETY: we just cleared all the tasks and we have &mut self
580-
unsafe { self.ready_to_run_queue.clear() };
583+
584+
// Note that at this point we could still have a bunch of tasks in the
585+
// ready to run queue. None of those tasks, however, have futures
586+
// associated with them so they're safe to destroy on any thread. At
587+
// this point the `FuturesUnordered` struct, the owner of the one strong
588+
// reference to the ready to run queue will drop the strong reference.
589+
// At that point whichever thread releases the strong refcount last (be
590+
// it this thread or some other thread as part of an `upgrade`) will
591+
// clear out the ready to run queue and free all remaining tasks.
592+
//
593+
// While that freeing operation isn't guaranteed to happen here, it's
594+
// guaranteed to happen "promptly" as no more "blocking work" will
595+
// happen while there's a strong refcount held.
581596
}
582597
}
583598

futures-util/src/stream/futures_unordered/ready_to_run_queue.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<Fut> ReadyToRunQueue<Fut> {
9494
//
9595
// # Safety
9696
//
97-
// - All tasks **must** have had their futures dropped already (by FuturesUnordered::clear_head_all)
97+
// - All tasks **must** have had their futures dropped already (by FuturesUnordered::clear)
9898
// - The caller **must** guarantee unique access to `self`
9999
pub(crate) unsafe fn clear(&self) {
100100
loop {
@@ -107,3 +107,16 @@ impl<Fut> ReadyToRunQueue<Fut> {
107107
}
108108
}
109109
}
110+
111+
impl<Fut> Drop for ReadyToRunQueue<Fut> {
112+
fn drop(&mut self) {
113+
// Once we're in the destructor for `Inner<Fut>` we need to clear out
114+
// the ready to run queue of tasks if there's anything left in there.
115+
116+
// All tasks have had their futures dropped already by the `FuturesUnordered`
117+
// destructor above, and we have &mut self, so this is safe.
118+
unsafe {
119+
self.clear();
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)