@@ -558,7 +558,7 @@ impl<Fut> FuturesUnordered<Fut> {
558
558
pub fn clear ( & mut self ) {
559
559
self . clear_head_all ( ) ;
560
560
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.
562
562
unsafe { self . ready_to_run_queue . clear ( ) } ;
563
563
564
564
self . is_terminated . store ( false , Relaxed ) ;
@@ -575,9 +575,24 @@ impl<Fut> FuturesUnordered<Fut> {
575
575
576
576
impl < Fut > Drop for FuturesUnordered < Fut > {
577
577
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.
578
582
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.
581
596
}
582
597
}
583
598
0 commit comments