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

Client.loop_stop contains a race-condition #872

Open
PromyLOPh opened this issue Nov 27, 2024 · 0 comments
Open

Client.loop_stop contains a race-condition #872

PromyLOPh opened this issue Nov 27, 2024 · 0 comments
Labels
Status: Available No one has claimed responsibility for resolving this issue.

Comments

@PromyLOPh
Copy link

Bug Description

Client.loop_stop expects that self._thread stays valid, but _thread_main unsets self._thread after exiting, causing a race-condition:

$ python3 test.py
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    client.loop_stop()
  File "src/paho/mqtt/client.py", line 2365, in loop_stop
    self._thread.join()
    ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'join'

Reproduction

Trigger it by applying this patch to paho-mqtt, which simply adds a wait, so _thread_main always wins the race:

diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py
index 4ccc869..dc01f5e 100644
--- a/src/paho/mqtt/client.py
+++ b/src/paho/mqtt/client.py
@@ -2360,6 +2360,7 @@ class Client:
             return MQTTErrorCode.MQTT_ERR_INVAL

         self._thread_terminate = True
+        time.sleep (2)
         if threading.current_thread() != self._thread:
             self._thread.join()

Then run this minimal MQTT client:

import time

from paho.mqtt.client import Client
from paho.mqtt.enums import CallbackAPIVersion, MQTTProtocolVersion

client = Client(CallbackAPIVersion.VERSION2, 'testclient', protocol=MQTTProtocolVersion.MQTTv5)

client.loop_start()
client.connect(host='localhost')

time.sleep (2)

client.disconnect()
client.loop_stop()

Environment

  • Python version: Python 3.11.2
  • Library version: Commit d45de37
  • Operating system (including version): Debian 12
  • MQTT server (name, version, configuration, hosting details): mosquitto 2.0.15 without any configuration.
@github-actions github-actions bot added the Status: Available No one has claimed responsibility for resolving this issue. label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Available No one has claimed responsibility for resolving this issue.
Projects
None yet
Development

No branches or pull requests

1 participant