mosquitto-client
mosquitto-client copied to clipboard
100% core use when waiting for messages
I am seeing an issue where loop_forever or loop_until_disconnect is spinning on a core, consuming 100% of a CPU.
I can reproduce it with the below example code:
$ cat Cargo.toml
[package]
name = "mosquitto-test"
version = "0.1.0"
authors = ["glen"]
edition = "2018"
[dependencies]
mosquitto-client = "0.1.5"
$ cat src/main.rs
extern crate mosquitto_client as mosq;
use mosq::Mosquitto;
fn main() {
let m = Mosquitto::new("allo");
m.will_set("test/will",b"finished!",0,false).expect("can't set will");
m.connect("localhost",1883).expect("can't connect");
let bonzo = m.subscribe("bonzo/#",0).expect("can't subscribe to bonzo");
let frodo = m.subscribe("frodo/#",0).expect("can't subscribe to frodo");
// not interested in any retained messages!
let mut mc = m.callbacks(());
mc.on_message(|_,msg| {
if ! msg.retained() {
if bonzo.matches(&msg) {
println!("bonzo {:?}",msg);
} else
if frodo.matches(&msg) {
println!("frodo {:?}",msg);
m.disconnect().unwrap();
}
}
});
m.loop_forever(200).expect("broken loop");
}
I am using ubuntu
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.4 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
And have the latest mosquitto packages installed.
$ apt list mosquitto*
Listing... Done
mosquitto/bionic-updates,bionic-security,now 1.4.15-2ubuntu0.18.04.3 amd64 [installed]
mosquitto-auth-plugin/bionic 0.0.7-2.1ubuntu3 amd64
mosquitto-clients/bionic-updates,bionic-security,now 1.4.15-2ubuntu0.18.04.3 amd64 [installed]
mosquitto-dbg/bionic-updates,bionic-security 1.4.15-2ubuntu0.18.04.3 amd64
mosquitto-dev/bionic-updates,bionic-updates,bionic-security,bionic-security,now 1.4.15-2ubuntu0.18.04.3 all [installed]
Note I don't see the spin using the mosquitto_sub command line tool, so there is some difference which I am trying to track down.
Apparently, this issue was already reported here: https://github.com/stevedonovan/mosquitto-client/issues/7
Maybe what we can do is write our self loop like that:
use std::{thread, time};
let ten_millis = time::Duration::from_millis(1000);
loop {
thread::sleep(ten_millis);
MQTT.do_loop(-1).unwrap();
}