Skip to content

Commit b5da08c

Browse files
authored
Merge pull request #1598 from Pauan/fix-futures
Fixing panic if the Future wakes up after returning Poll::Ready
2 parents 379cad0 + 5a1dfdf commit b5da08c

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

crates/futures/src/futures_0_3.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -233,27 +233,27 @@ where
233233

234234
match lock.pop_front() {
235235
Some(task) => {
236-
let mut future = task.future.borrow_mut();
237-
238-
let poll = {
239-
// This will only panic if the Future wakes up the Waker after returning Poll::Ready
240-
let mut future = future.as_mut().unwrap_throw();
241-
242-
// Clear `is_queued` flag so that it will re-queue if poll calls waker.wake()
243-
task.is_queued.set(false);
244-
245-
// This is necessary because the polled task might queue more tasks
246-
drop(lock);
247-
248-
// TODO is there some way of saving these so they don't need to be recreated all the time ?
249-
let waker = ArcWake::into_waker(task.clone());
250-
let cx = &mut Context::from_waker(&waker);
251-
Pin::new(&mut future).poll(cx)
252-
};
253-
254-
if let Poll::Ready(_) = poll {
255-
// Cleanup the Future immediately
256-
*future = None;
236+
let mut borrow = task.future.borrow_mut();
237+
238+
// This will only be None if the Future wakes up the Waker after returning Poll::Ready
239+
if let Some(future) = borrow.as_mut() {
240+
let poll = {
241+
// Clear `is_queued` flag so that it will re-queue if poll calls waker.wake()
242+
task.is_queued.set(false);
243+
244+
// This is necessary because the polled task might queue more tasks
245+
drop(lock);
246+
247+
// TODO is there some way of saving these so they don't need to be recreated all the time ?
248+
let waker = ArcWake::into_waker(task.clone());
249+
let cx = &mut Context::from_waker(&waker);
250+
Pin::new(future).poll(cx)
251+
};
252+
253+
if let Poll::Ready(_) = poll {
254+
// Cleanup the Future immediately
255+
*borrow = None;
256+
}
257257
}
258258
},
259259
None => {

0 commit comments

Comments
 (0)