fluffysquirrels / mqtt-async-client-rs

An MQTT client written in Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem about 100% CPU usage

sunnysab opened this issue · comments

Hi, guys!
I encountered a problem when I stop the broker, the mosquitto service, my CPU usage would rise to 100% in a few seconds.
I was puzzled and tried to use a flame graph to look for what happened.
My code:

async fn mqtt_loop(db: &PgPool, mut client: &mut Client) {
    loop {
        if let Ok(message) = client.read_subscriptions().await {
            if let Err(e) = on_message(db, &message).await {
                warn!("Could not resolve message: {}", e);
            }
        } else {
            warn!("The connection to the mqtt server lost.");
            // Loop and reconnect. Break when connected.
            loop {
                if client.connect().await.is_ok() {
                    break;
                }
                tokio::time::delay_for(Duration::from_secs(10)).await;
            }
            warn!("Reconnected to the mqtt server.");
            // Conntected.
            let subscribe_result = subscribe_message(&mut client).await;
            if subscribe_result.is_err() {
                warn!("Could not subscribe from mqtt server.");
            }
        }
    } // End of loop.
}

If you start the mosquitto service again at this time, it has no effect. (Maybe I need to reconnect).
Below is the flamegraph, my program named "server", and mqtt_main is the function which does initialization and calls mqtt_loop.
图片

I need your help. Thanks a lot!

Hi, thanks for your interest in the crate.

Can you switch on debug logging with env_logger by setting environment variable RUST_LOG to "debug", then show the output please?

Closing as unfortunately we didn't get enough info to track down the issue. Feel free to re-open this issue or create a new one with logs.