Will select!() with pending() leak?

Hi there,
I actually have a usecase for futures::pending() for something that may or may not expire on its own.

So I select!() against two futures and the (possibly eternal) timeout like this:

let sleep = async {
    if let Some(timeout_before_close) = timeout_before_close {
        async_std::task::sleep(timeout_before_close).await;
    } else {
        pending::<()>().await;
    }
};

// phase 2: wait for user input (action or close) or expiration
let (reason, action) = select! {
    action = action_rx.recv().fuse() => {
        log::trace!("close from action");
        (CloseReason::CloseAction, action.ok())
    }
    reason = close_rx.recv().fuse() => {
        log::trace!("close from user");
        (reason.unwrap_or(CloseReason::Expired), None)
    }
    _ = sleep.fuse() => {
        log::trace!("close from expire after {timeout_before_close:?}", );
        (CloseReason::Expired, None)
    }
};

all of this runs inside a task, the handle of which I just drop.
my concern now:

does select!() clean up the pending future if either close_rx or action_rx resolve or will this leak?

Thank you

Tldr is that select doesn't allocate or otherwise have any side effects once it returns. The way async works in rust is that normally the only things that have to worry about leaks are when you're implementing the Future directly,and then it's "just" a matter of implementing Drop correctly, the same as usual.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.