matrix-org / matrix-rust-sdk

Matrix Client-Server SDK for Rust
Apache License 2.0
1.2k stars 240 forks source link

matrix_sdk::room::Room function send() fails without returning a Result #4043

Open CodingSamS opened 8 hours ago

CodingSamS commented 8 hours ago

OS: Debian 12 build target: x86_64-unknown-linux-gnu

Hello,

I need some help debugging the usage of matrix-sdk room.

Login is done already and also the code below works as expected. When another thread sends a string to the std::sync::mpsc::Receiver, I receive the string and am able to forward it to the matrix room (and can confirm on my matrix client that it is indeed sent there).

// listen for events to send
loop {
    debug!("wait for event");
    match rx.recv() {
        Ok(message) => {
            debug!("matrix room boot received message");
            match retry(Fixed::from_millis(5000).take(12), || {
                match client.get_room(room_id) {
                    Some(room) => Ok(room),
                    None => Err("room not found"),
                }
            }) {
                Ok(room) => {
                    debug!("room found");
                    match room
                        .send(RoomMessageEventContent::text_plain(&message))
                        .await
                    {
                        Ok(_) => debug!("message sent successfully to matrix room"),
                        Err(error) => error!(
                            "sending message to matrix room failed with error: {}",
                            error
                        ),
                    };
                }
                Err(_) => {
                    error!("Finding the room failed");
                    exit(1)
                }
            };
        }
        Err(error) => {
            error!("Channel closed with error: {}", error);
            exit(1);
        }
    }
}

But when the receiver doesn't receive something for a few minutes and then again receives something, the room.send panics internally somehow. See this log for reference:

Sep 28 14:47:50 matrix email-to-matrix[62069]: [2024-09-28T14:47:50Z DEBUG email_to_matrix] send handler->matrix thread: successful
Sep 28 14:47:50 matrix email-to-matrix[62069]: [2024-09-28T14:47:50Z DEBUG email_to_matrix] matrix room boot received message
Sep 28 14:47:50 matrix email-to-matrix[62069]: [2024-09-28T14:47:50Z DEBUG email_to_matrix] room found
Sep 28 14:47:51 matrix email-to-matrix[62069]: [2024-09-28T14:47:51Z DEBUG email_to_matrix] message sent successfully to matrix room
Sep 28 14:47:51 matrix email-to-matrix[62069]: [2024-09-28T14:47:51Z DEBUG email_to_matrix] wait for event
Sep 28 14:47:59 matrix email-to-matrix[62069]: [2024-09-28T14:47:59Z DEBUG email_to_matrix] send handler->matrix thread: successful
Sep 28 14:47:59 matrix email-to-matrix[62069]: [2024-09-28T14:47:59Z DEBUG email_to_matrix] matrix room boot received message
Sep 28 14:47:59 matrix email-to-matrix[62069]: [2024-09-28T14:47:59Z DEBUG email_to_matrix] room found
Sep 28 14:47:59 matrix email-to-matrix[62069]: [2024-09-28T14:47:59Z DEBUG email_to_matrix] message sent successfully to matrix room
Sep 28 14:47:59 matrix email-to-matrix[62069]: [2024-09-28T14:47:59Z DEBUG email_to_matrix] wait for event
Sep 28 14:48:29 matrix email-to-matrix[62069]: [2024-09-28T14:48:29Z DEBUG email_to_matrix] send handler->matrix thread: successful
Sep 28 14:48:29 matrix email-to-matrix[62069]: [2024-09-28T14:48:29Z DEBUG email_to_matrix] matrix room boot received message
Sep 28 14:48:29 matrix email-to-matrix[62069]: [2024-09-28T14:48:29Z DEBUG email_to_matrix] room found
Sep 28 14:48:29 matrix email-to-matrix[62069]: [2024-09-28T14:48:29Z DEBUG email_to_matrix] message sent successfully to matrix room
Sep 28 14:48:29 matrix email-to-matrix[62069]: [2024-09-28T14:48:29Z DEBUG email_to_matrix] wait for event
Sep 28 14:54:02 matrix email-to-matrix[62069]: [2024-09-28T14:54:02Z DEBUG email_to_matrix] send handler->matrix thread: successful
Sep 28 14:54:02 matrix email-to-matrix[62069]: [2024-09-28T14:54:02Z DEBUG email_to_matrix] matrix room boot received message
Sep 28 14:54:02 matrix email-to-matrix[62069]: [2024-09-28T14:54:02Z DEBUG email_to_matrix] room found
Sep 28 15:01:55 matrix email-to-matrix[62069]: [2024-09-28T15:01:55Z WARN  email_to_matrix] send handler->matrix thread: failed with error: sending on a closed channel

14:47 - 14:48 shows the succesful room messages. The message on 14:54:02 is then the one that fails:

According to the log I reach the part of the matching block (debug message "room found")

match room
  .send(RoomMessageEventContent::text_plain(&message))
  .await
{
    Ok(_) => debug!("message sent successfully to matrix room"),
    Err(error) => error!(
        "sending message to matrix room failed with error: {}",
        error
    ),
};

But I neither get the Ok log message message sent successfully to matrix room nor the Error log message ending message to matrix room failed with error. And the whole tokio thread where this loop is running on terminates (or hangs within this send call?). Because when the next message (15:01:55) is sent through the std::sync::mpsc::Channel the sender fails because the channel is closed. And my matrix room thread from above holds the only receiver for this std::sync::mpsc::Channel.

I confirmed this behaviour with the crates.io version (0.7.1) and the git version.

Do you have any leads what might cause this behaviour?