@@ -102,15 +102,19 @@ impl Executor {
102
102
future : impl Future < Output = T > + Send + ' static ,
103
103
) -> Task < T > {
104
104
let mut active = self . state ( ) . active . lock ( ) . unwrap ( ) ;
105
+
106
+ // Remove the task from the set of active tasks when the future finishes.
105
107
let index = active. next_vacant ( ) ;
106
108
let state = self . state ( ) . clone ( ) ;
107
109
let future = async move {
108
110
let _guard = CallOnDrop ( move || drop ( state. active . lock ( ) . unwrap ( ) . remove ( index) ) ) ;
109
111
future. await
110
112
} ;
111
113
114
+ // Create the task and register it in the set of active tasks.
112
115
let ( runnable, task) = async_task:: spawn ( future, self . schedule ( ) ) ;
113
116
active. insert ( runnable. waker ( ) ) ;
117
+
114
118
runnable. schedule ( ) ;
115
119
task
116
120
}
@@ -304,15 +308,19 @@ impl LocalExecutor {
304
308
/// ```
305
309
pub fn spawn < T : ' static > ( & self , future : impl Future < Output = T > + ' static ) -> Task < T > {
306
310
let mut active = self . inner ( ) . state ( ) . active . lock ( ) . unwrap ( ) ;
311
+
312
+ // Remove the task from the set of active tasks when the future finishes.
307
313
let index = active. next_vacant ( ) ;
308
314
let state = self . inner ( ) . state ( ) . clone ( ) ;
309
315
let future = async move {
310
316
let _guard = CallOnDrop ( move || drop ( state. active . lock ( ) . unwrap ( ) . remove ( index) ) ) ;
311
317
future. await
312
318
} ;
313
319
320
+ // Create the task and register it in the set of active tasks.
314
321
let ( runnable, task) = async_task:: spawn_local ( future, self . schedule ( ) ) ;
315
322
active. insert ( runnable. waker ( ) ) ;
323
+
316
324
runnable. schedule ( ) ;
317
325
task
318
326
}
0 commit comments