Content-Length: 269527 | pFad | http://github.com/eclipse-paho/paho.mqtt.python/issues/872

0E Client.loop_stop contains a race-condition · Issue #872 · eclipse-paho/paho.mqtt.python · GitHub
Skip to content

Client.loop_stop contains a race-condition #872

Open
@PromyLOPh

Description

@PromyLOPh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: AvailableNo one has claimed responsibility for resolving this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: http://github.com/eclipse-paho/paho.mqtt.python/issues/872

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy