Should locks be dropped before calling waker.wake()?

Yes, you should drop locks before calling wake. The task being polled inside wake would be very unusual, but it doesn't matter because there is a different thing that can happen inside the call to wake: The future could be dropped.

This is because the waker might be holding the last strong reference to the future, and the call to wake() consumes that waker. There are many examples of futures that lock mutexes inside their destructors, for example this is the case with many of Tokio's IO futures, since they often build a linked list inside the futures themselves. This is valid because the futures are pinned, but if you drop the future, you must remove that future from the linked list, which can involve locking a mutex.

7 Likes