1technophile / OpenMQTTGateway

MQTT gateway for ESP8266 or ESP32 with bidirectional 433mhz/315mhz/868mhz, Infrared communications, BLE, Bluetooth, beacons detection, mi flora, mi jia, LYWSD02, LYWSD03MMC, Mi Scale, TPMS, BBQ thermometer compatibility & LoRa.
https://docs.openmqttgateway.com
GNU General Public License v3.0
3.62k stars 795 forks source link

Add semaphore xMqttMutex to protect mqtt->publish() and mqtt->loop() from conflicts with other concurrent threads #2024

Closed puterboy closed 2 months ago

puterboy commented 2 months ago

Description:

Protecting mqtt->publish() fixed the problem with corruption of MQTT discovery config messages https://github.com/1technophile/OpenMQTTGateway/issues/2012 Protecting mqtt->loop() seems like good practice since that could potentially conflict with another thread running mqtt->publish

Note I left mqtt->connect unprotected since I think that is unlikely to lead to data corruption or race conditions. Similarly, I left mqtt->begin, mqtt->disconnect, mqtt->stop, mqtt->subscribe and mqtt->will unprotected since they occur either during setup or just prior to esp restart so I think again corruption and race conditions are unlikely.

Checklist: