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

AttributeError: 'NoneType' object has no attribute 'recv' #345

Closed
weswitt opened this issue Dec 5, 2018 · 11 comments
Closed

AttributeError: 'NoneType' object has no attribute 'recv' #345

weswitt opened this issue Dec 5, 2018 · 11 comments

Comments

@weswitt
Copy link

weswitt commented Dec 5, 2018

I'm running version 1.4 (latest). Code is running on Ubuntu 18.04 in a VirtualBox VM.

I'm seeing the following exception and it happens very reliably. The app does not run very long before this throws.

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2913, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1578, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1072, in loop
    rc = self.loop_read(max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1374, in loop_read
    rc = self._packet_read()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2030, in _packet_read
    byte = self._sock_recv(1)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 607, in _sock_recv
    return self._sock.recv(bufsize)
AttributeError: 'NoneType' object has no attribute 'recv'
@weswitt
Copy link
Author

weswitt commented Dec 5, 2018

Dropping back to 1.3.1 and this problem does not happen. Seems like a bug introduced in 1.4

@sousacanfly
Copy link

I can confirm, the same's happening here.
Using latest version too.

@weswitt
Copy link
Author

weswitt commented Dec 9, 2018

With some more debugging I worked around this. The issue was caused by the fact that I was using the multi-threaded loop_forever. What happens is that the socket connection is dropped and then my main thread called reconnect. This causes the socket object to be set to None (null) and then you see this error. I changed my code to use the single threaded loop and it no longer happens. That being said, this is a bug in the paho mqtt code.

Also, my previous statement about 1.3.1 fixing it was false. It also happens with that version.

@PierreF
Copy link
Contributor

PierreF commented Dec 16, 2018

loop_forever could not and should not be run in multiple thread. We should either add some note in the documentation and/or some kind of assert in the code that reject running loop_forever twice.

@weswitt
Copy link
Author

weswitt commented Dec 16, 2018

You misread my comment. I did and would not run loop_forever twice.

@PierreF
Copy link
Contributor

PierreF commented Dec 16, 2018

What do you mean by the multi-threaded loop_forever ?

@weswitt weswitt closed this as completed Dec 19, 2018
@kitten77
Copy link

@PierreF

ERROR asyncio    Exception in callback None()
handle: <Handle cancelled>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/home/muad/workbench/tonder/irc/plugins/mqtt.py", line 30, in cb
    client.loop_read()
  File "/home/muad/venv/tonder3.7/lib/python3.7/site-packages/paho/mqtt/client.py", line 1374, in loop_read
    rc = self._packet_read()
  File "/home/muad/venv/tonder3.7/lib/python3.7/site-packages/paho/mqtt/client.py", line 2030, in _packet_read
    byte = self._sock_recv(1)
  File "/home/muad/venv/tonder3.7/lib/python3.7/site-packages/paho/mqtt/client.py", line 607, in _sock_recv
    return self._sock.recv(bufsize)
AttributeError: 'NoneType' object has no attribute 'recv'

Any ideas on what is causing this?

@emha69
Copy link

emha69 commented Jan 30, 2020

This seems to be caused if the client.loop(n>1), and if the client.loop() is not used immediately after client.publish().
For example, when the client.loop() is used in the main loop e.g. for waiting for messages or something like that.

@bggardner
Copy link

I had this error when using loop_start() and calling connect() more than once, not realizing the client auto-reconnects. So, not calling connect() again was my solution.

@mxmaxime
Copy link

mxmaxime commented Sep 1, 2021

Got the same issue here, sometimes it works sometimes not... After a bit of research I found that I had loop_start() and after a loop_forever(), removing one of them worked.

I've searched a bit in the library, I don't understand how self._sock could be None, it's only set to None in the method _sock_close(). I didn't searched any further, but in my understanding:

  • checks on self._sock is not None are performed before calling self._sock_recv, but in between another thread set it to None by calling _sock_close().

@russell-sealand
Copy link

I fixed the problem by switching to use client.loop_start() instead of client.loop_forever()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants