Robotics StackExchange | Archived questions

AttributeError: 'NoneType' object has no attribute 'close'

issue details

Exception in thread /iiwa/joint_states:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros_pubsub.py", line 185, in robust_connect_subscriber
    conn.receive_loop(receive_cb)           
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros_base.py", line 846, in receive_loop
    self.close()
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros_base.py", line 858, in close
    self.socket.close()
AttributeError: 'NoneType' object has no attribute 'close'

The above error raised when I subscribe a topic in a sub-thread (python), and init_node is defined in main-thread.

main thread:

rospy.init_node('node_name')

sub thread:

js = rospy.wait_for_message('/iiwa/joint_states', JointState, timeout=5)

Similar issue can be found at:

ros_comm/issues/2212,

Debugging - TCP/IP connection failed errors

I think it is the multi-thread caused the problem, workaround in ros_comm/issues/2212 can't be applied in my case directly.

BTW, this error raised only occasionally, and I can publish other topic in sub thread normally without any problem.

I can publish topic in sub

Thanks in advance!

Asked by lyh458 on 2022-07-09 05:02:50 UTC

Comments

Answers

workaround in ros_comm/issues/2212 can't be applied in my case directly.

Why not? Wrap your line of code with a try/except and set js = None in the exception path.

I'm surprised to see the developer saying that tcp_ros is not thread-safe; I always assumed it was.

Edit: The better workaround would be to move this line of code to your main thread, where presumably it won't throw the exception.

Asked by Mike Scheutzow on 2022-07-09 09:15:51 UTC

Comments

Thank you for the answer!

I made a simple test (single nodes without multi-thread), here is the code:

import rospy
from sensor_msgs.msg import JointState

import rospy
from sensor_msgs.msg import JointState

def main():
    rospy.init_node('test', anonymous=True)
    while not rospy.is_shutdown():
        js = rospy.wait_for_message('/iiwa/joint_states', JointState, timeout=5)
        print(list(js.position))

if __name__ == '__main__':
    main()

However, same error raised. Is that means the function wait_for_message has something wrong itself?

Asked by lyh458 on 2022-07-13 03:40:02 UTC

I tried both melodic and noetic, and your code in the immediately-above comment does not throw any exception for me. Of course, a timeout exception will be thrown if nothing is being published to the topic name.

Asked by Mike Scheutzow on 2022-07-13 11:17:55 UTC