Skip to content

MQTT client hard crash when configuring a last will message on esp32 #587

Description

@ApolloRoboto

Bug description

MQTT client hard crash when configuring a last will message on esp32 when creating the client.

Full stack trace here:

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x400014dc  PS      : 0x00060830  A0      : 0x80001459  A1      : 0x3ffbba30
A2      : 0x00000001  A3      : 0xfffffffd  A4      : 0x000000ff  A5      : 0x0000ff00
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x8008b6f2  A9      : 0x3ffbba00
A10     : 0x3ffafc50  A11     : 0x3ffc870c  A12     : 0x00000019  A13     : 0x3ffc89ac
A14     : 0x00000000  A15     : 0x656e696c  SAR     : 0x00000016  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000001  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000


Backtrace: 0x400014d9:0x3ffbba30 0x40001456:0x3ffbba40 0x40001445:0x3ffbba60 0x40142882:0x3ffbba80 0x40142ebc:0x3ffbbac0 0x400db13c:0x3ffbbb10 0x400d5a68:0x3ffbbce0 0x400d6fdb:0x3ffbbd00 0x40199a93:0x3ffbbec0 0x400d6dec:0x3ffbbee0 0x400df894:0x3ffbbf00 0x400df837:0x3ffbbf30 0x400ea87b:0x3ffbbf70 0x400d6ddc:0x3ffbbfb0 0x400d713b:0x3ffbbfe0 0x400ded27:0x3ffbc000 0x401a0ef3:0x3ffbc020
0x40142882 - esp_mqtt_set_config
    at /mnt/c/Users/Apollo/Repositories/rust-esp32-led-panel/.embuild/espressif/esp-idf/v5.3.2/components/mqtt/esp-mqtt/mqtt_client.c:464
0x40142ebc - esp_mqtt_client_init
    at /mnt/c/Users/Apollo/Repositories/rust-esp32-led-panel/.embuild/espressif/esp-idf/v5.3.2/components/mqtt/esp-mqtt/mqtt_client.c:867
0x400db13c - esp_idf_svc::mqtt::client::EspMqttClient::new_raw
    at /home/apollo/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/esp-idf-svc-0.51.0/src/mqtt/client.rs:480
0x400d5a68 - esp_idf_svc::mqtt::client::EspMqttClient::new_nonstatic_cb
    at /home/apollo/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/esp-idf-svc-0.51.0/src/mqtt/client.rs:435
0x400d6fdb - rust_esp32_led_panel::main
    at /mnt/c/Users/Apollo/Repositories/rust-esp32-led-panel/src/main.rs:60
0x40199a93 - core::ops::function::FnOnce::call_once
    at /home/apollo/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/ops/function.rs:250
0x400d6dec - std::rt::lang_start::{{closure}}
    at /home/apollo/.rustup/toolchains/esp/lib/rustlib/src/rust/library/std/src/rt.rs:199
0x400df894 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
    at /home/apollo/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/ops/function.rs:284
0x400df837 - std::panic::catch_unwind
    at /home/apollo/.rustup/toolchains/esp/lib/rustlib/src/rust/library/std/src/panic.rs:359
0x400ea87b - std::panic::catch_unwind
    at /home/apollo/.rustup/toolchains/esp/lib/rustlib/src/rust/library/std/src/panic.rs:359
0x400d6ddc - std::rt::lang_start
    at /home/apollo/.rustup/toolchains/esp/lib/rustlib/src/rust/library/std/src/rt.rs:198
0x400d713b - main
    at ??:??
0x400ded27 - app_main
    at /home/apollo/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/esp-idf-sys-0.36.1/src/start.rs:46
0x401a0ef3 - main_task
    at /mnt/c/Users/Apollo/Repositories/rust-esp32-led-panel/.embuild/espressif/esp-idf/v5.3.2/components/freertos/app_startup.c:208

Would you like to work on a fix?
no, I'm not familiar with the source enough

To Reproduce

Here's a minimal main.rs file.

Note that using MqttClientConfiguration::default(); doesn't crash.

use esp_idf_hal::peripherals::Peripherals;
use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfiguration, QoS};
use log::info;

// wifi example from https://github.com/esp-rs/std-training/blob/main/common/lib/wifi/src/lib.rs
mod wifi;

#[derive(Debug)]
#[toml_cfg::toml_config]
pub struct Config {
    #[default("localhost")]
    mqtt_host: &'static str,
    #[default("1883")]
    mqtt_user: &'static str,
    #[default("")]
    mqtt_pass: &'static str,
    #[default("")]
    wifi_ssid: &'static str,
    #[default("")]
    wifi_pass: &'static str,
}

fn main() -> ! {
    esp_idf_svc::sys::link_patches();
    esp_idf_svc::log::EspLogger::initialize_default();

    let sysloop = EspSystemEventLoop::take().unwrap();
    let peripherals = Peripherals::take().unwrap();

    let _wifi = wifi::wifi(
        CONFIG.wifi_ssid,
        CONFIG.wifi_pass,
        peripherals.modem,
        sysloop,
    )
    .unwrap();

    // let mqtt_config = MqttClientConfiguration::default();
    let mqtt_config = MqttClientConfiguration {
        lwt: Some(LwtConfiguration {
            topic: "dt/device/status/offline",
            qos: QoS::AtLeastOnce,
            retain: false,
            payload: &[],
        }),
        ..Default::default()
    };

    let mqtt_broker_url = match CONFIG.mqtt_user.is_empty() {
        false => format!("mqtt://@{}", CONFIG.mqtt_host),
        true => format!(
            "mqtt://{}:{}@{}",
            CONFIG.mqtt_user, CONFIG.mqtt_pass, CONFIG.mqtt_host
        ),
    };

    // crashes here
    let mut mqtt_client =
        EspMqttClient::new_cb(&mqtt_broker_url, &mqtt_config, move |_message_event| {
            info!("Recived a message event");
        })
        .expect("Couldn't create a new mqtt client");

    mqtt_client
        .publish("dt/device/status/online", QoS::AtLeastOnce, false, &[])
        .unwrap();

    loop {
        // do nothing
    }
}
cargo build
espflash flash --monitor .\target\xtensa-esp32-espidf\debug\rust-esp32-led-panel

Expected behavior

Not to crash and is connected to the MQTT broker with a last will.

Environment

  • Crate (esp-idf-svc) version: 0.51.0
  • ESP-IDF branch or tag: v5.3.2
  • Target device (MCU): esp32
  • OS: WSL2 Ubuntu (Windows 10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions