mlesniew / PicoMQTT

ESP MQTT client and broker library
GNU Lesser General Public License v3.0
219 stars 25 forks source link

question on begin_publish/send API #38

Closed mhaberler closed 3 weeks ago

mhaberler commented 1 month ago

given the following code is called from loop() using the mutexes branch:

    auto publish = mqtt.begin_publish("gps/nav", measureJson(json));
    serializeJson(json, publish);
    publish.send();

    // if (li.status == LS_VALID) {
    //     json.clear();
    //     json["time"] = micros() * 1.0e-6;
    //     json["meters"] = li.elevation;
    //     json["lat"] = ub_nav_pvt.lat * 1e-7;
    //     json["lon"] = ub_nav_pvt.lon * 1e-7;
    //     auto dempublish = mqtt.begin_publish("dem/elevation", measureJson(json));
    //     serializeJson(json, dempublish);
    //     dempublish.send();
    // }

if I uncomment the commented code, the server deadlocks in mqtt.begin_publish

doing a plain mqtt.publish() twice in a row works fine

Am I doing something wrong? how would I properly do this?

thanks in advance

Michael

mlesniew commented 1 month ago

You're doing everything right, it looks like there's a bug somewhere. Calling publish.send() should release a lock, but apparently it doesn't.

The mutexes branch has quick and dirty changes mainly for debugging, I haven't tested it much.

Still, I'll have a look to see if this can be fixed easily.

mlesniew commented 1 month ago

Oh I just realized what it could be!

Iirc the internal lock is released when the publish object is destroyed. Here it's still in scope when you're calling begin_publish for the second time.

Try surrounding the first block of code in curly braces. This way publish will be destroyed earlier.

mhaberler commented 1 month ago

ah, comprende, makes sense - will report.

mhaberler commented 1 month ago

confirmed to work with your suggestion.