ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

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

asked 2022-07-09 05:02:50 -0500

lyh458 gravatar image

updated 2022-07-10 01:03:09 -0500

gvdhoorn gravatar image

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!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-07-09 09:15:51 -0500

Mike Scheutzow gravatar image

updated 2022-07-10 09:23:06 -0500

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.

edit flag offensive delete link more

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?

lyh458 gravatar image lyh458  ( 2022-07-13 03:40:02 -0500 )edit

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.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-07-13 11:17:55 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-07-09 05:02:50 -0500

Seen: 1,194 times

Last updated: Jul 10 '22