Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mqtt_cxx] Race condition in idf::mqtt::Client constructor (IDFGH-13475) #631

Open
3 tasks done
snake-4 opened this issue Aug 15, 2024 · 0 comments
Open
3 tasks done

Comments

@snake-4
Copy link

snake-4 commented Aug 15, 2024

Answers checklist.

  • I have read the documentation for esp-protocols components and the issue is not addressed there.
  • I have updated my esp-protocols branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

There is a race condition in the constructor of the Client class, where the event handler may be invoked before subclass of Client is done constructing. This results in the program terminating if the dispatched event is MQTT_EVENT_CONNECTED or MQTT_EVENT_DATA since on_data and on_connected are pure virtual functions.

Pure virtual functions cannot be called before the object is fully constructed, otherwise a compiler generated termination function will be called.

The exact line is below:

CHECK_THROW_SPECIFIC(esp_mqtt_client_start(handler.get()), mqtt::MQTTException);

This cannot be solved by making the aforementioned functions normal virtual functions, as that would result in an undelivered event instead. A lock in the constructor would also not work as the base class can't know when the subclass is done constructing. Preferably the MQTT client should have a separate function for starting the underlying client and this should not be done in the constructor.

We discovered this while integration testing our code on the Linux target with a MQTT broker on the localhost. The connection is made so fast that the subclass doesn't have time to finish constructing before an MQTT_EVENT_CONNECTED event is dispatched.

@github-actions github-actions bot changed the title [mqtt_cxx] Race condition in idf::mqtt::Client constructor [mqtt_cxx] Race condition in idf::mqtt::Client constructor (IDFGH-13475) Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants