Skip to content

If future returns Pending, it must store waker #6314

Open
@MikailBag

Description

@MikailBag

What it does

If future returns Pending, it should use the provided waker to notify executor that it can be polled again.
If the waker is not stored, the future will be unable to notify it and therefore it will be never polled again. Such a future is probably incorrect.

What is verified

(Probably imprecise, so just a proposal.)
For each MIR block B0 whose terminator is return clippy should check that there is least one maybe-predecessor statement (i.e. defined in a block B1, and B0 is reachable from B1 in CFG) which somehow uses cx.

Categories (optional)

  • Kind: correctness

Drawbacks

False-positive here is a future that sometimes diverges and sometimes terminates (because if a future always diverges that programmer should use futures_util::future::pending() instead). I think such features are rare but don't have any estimations or experiments.

Example

impl Future for MyFut {
    type Output = ...;
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match self.state() {
            // ...
            State::Foo => {
                  // oops, we've forgotten to store waker, future will be never polled again
                  Poll::Pending
             }
        }
   }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-hardCall for participation: This a hard problem and requires more experience or effort to work onT-MIRType: This lint will require working with the MIR

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions