eclipse / paho.mqtt.python

paho.mqtt.python
Other
2.17k stars 723 forks source link

Handle edge case where socket is None #686

Closed RobertDeRose closed 8 months ago

RobertDeRose commented 1 year ago
Exception has occurred: AttributeError
'NoneType' object has no attribute 'recv'
  File "paho/mqtt/client.py", line 640, in _sock_recv

Fixes https://github.com/eclipse/paho.mqtt.python/issues/505

RobertDeRose commented 1 year ago

@ralight I've search the issues and see others have reports this issue and stated that calling loop_start instead of loop_forver, however, loop_start just calls loop_forever on its own and this issue can happen and its not possible to catch it as it's inside an unmanaged thread.

This might not be to the correct fix, but it fixes it for me and I would assume others as if the Socket is None, there is no connection and the reconnect logic seems to kick in

RobertDeRose commented 1 year ago

@ralight and @PierreF hate to be a bother here, but this is causing me some headaches in a production environment and while I could make a fork and build my own version, It would be nice if one of the maintainers could confirm that this appears to be the correct fix. Thanks!

gabrielgbs97 commented 1 year ago

Would like to see this one on main, had same issue... Still couldn't face the root of cause

RobertDeRose commented 1 year ago

@gabrielgbs97

I gave up getting this PR merged. I ended up doing my own thread management

Just start your own thread, call loop_forver inside a try..except block and recall loop_fover if an exception is thrown The thread is simple a while True: around the try...except

lucasjinreal commented 1 year ago

@ralight Please merge this PR, this caused seriously randomly issues in our production!

PierreF commented 8 months ago

Thank for your contribution.

As I said in #505, I think the real fix would be to audit concurrency access and improve either locking or improve documentation on what could be called concurrently. In the meantime your fix seems simple and correct the the described problem.