Skip to content

EventLoop cannot gracefully exit because it holds a request_tx #975

@bitcapybara

Description

@bitcapybara

Hello everyone! I noticed that the ConnectionError::RequestsDone error in rumqttc can be used to implement graceful program shutdown, but during actual runtime, after AsyncClient exits, the EventLoop still holds request_tx, preventing this error from being caught. Moreover, the request_tx held by EventLoop doesn't seem to serve any purpose within the EventLoop itself. Could it be removed to ensure that only AsyncClient holds request_tx?

In the EventLoop::next_request method, when all senders are dropped, it returns a ConnectionError::RequestsDone error

async fn next_request(
    pending: &mut VecDeque<Request>,
    rx: &Receiver<Request>,
    pending_throttle: Duration,
) -> Result<Request, ConnectionError> {
    if !pending.is_empty() {
        time::sleep(pending_throttle).await;
        // We must call .next() AFTER sleep() otherwise .next() would
        // advance the iterator but the future might be canceled before return
        Ok(pending.pop_front().unwrap())
    } else {
        match rx.recv_async().await {
            Ok(r) => Ok(r),
            Err(_) => Err(ConnectionError::RequestsDone),
        }
    }
}

If this issue is accepted, I'd be happy to submit a PR on this issue. 😊

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions