Is it possible for the executor to know that a suspended async task can be removed and its local variables can be cleared from the stack if there are now wakers to the suspended task remaining in other parts of program memory?
Yes, Waker
s have to be reference counted (in most cases) and so it is possible for the executor to drop the task when it detects that the reference count is zero. It is not specified whether executors are required to do this, though — std::task
doesn’t actually define what properties a task must have besides polling a Future
when the Waker
is used, and a future that never wakes might be used intentionally to retain some resources until cancelled.
You mean that there is the possibility for a future who does not store its waker when polled anywhere? Isn’t that a useless future?
Sometimes — rarely, but sometimes — what you want is for whatever your caller will do when you return not to happen.
There's even a std
function for it: std::future::pending()
.
Yes. Tokio will in fact do so if you call abort
on the task.
I'm confused about what you’re claiming here. How does abort()
affect the behavior in response to dropping the last Waker
? Surely it drops the task regardless of what the task did with its waker?
I may have misunderstood the question. Tokio doesn't clean up tasks when there are no wakers. But tokio-console can detect it with a "this task has lost it's waker" warning.