[ERROR] bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object>> ; RuntimeError: dictionary changed size during iteration

asked 2022-09-21 10:53:18 -0600

Nagarjun gravatar image

updated 2022-09-22 01:06:35 -0600

ravijoshi gravatar image

I am working on camera calibration ros node. When there is a good sample the node takes the good sample into cache. At that time I am writing images of that frame into my local system and trying to compare if images written at that moment are similar to images being subscribed during the node initialization of the camera_calibrator script file. For that I have written a message filter, i.e. register callback function.

msub = message_filters.Subscriber('image', sensor_msgs.msg.Image)
sub1 = message_filters.Subscriber('/webcam', sensor_msgs.msg.Image)
self.ts = message_filters.ApproximateTimeSynchronizer([msub,sub1], 10,0.1,allow_headerless=True)

Later somewhere inside my code I call:


def image_callback(msg1, msg2, cnt):
    print("Received an image!")
        # Convert your ROS Image message to OpenCV2
        cv2_img2 = bridge.imgmsg_to_cv2(msg2, "bgr8")
        cv2_img1 = bridge.imgmsg_to_cv2(msg1, "bgr8")
    except CvBridgeError as e:
        # Save your OpenCV2 image as a jpeg 
        cv2.imwrite('/home/test_' + str(cnt)+ '.png', cv2_img2)
        cv2.imwrite('/home/ori_time_' + str(cnt)+ '.png', cv2_img1)

It does write images but I get following error after few images are wrtiiten:

[ERROR] [1663774344.611751]: bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object at 0x7f47e6ba6190>>
Traceback (most recent call last):
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 750, in _invoke_callback
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 76, in callback
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 58, in signalMessage
    cb(*(msg + args))
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 330, in add
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 57, in signalMessage
    for (cb, args) in self.callbacks.values():
RuntimeError: dictionary changed size during iteration

I am unsure why I am getting this. I also tried

self.ts = message_filters.TimeSynchronizer([msub,sub1], 1)

It didn't work. Anyone knows why I am getting runtime error? How to solve this?

I am publishing these images from webcam continuously.

edit retag flag offensive close merge delete


  • For checking only, try removing cnt to make it self.ts.registerCallback(image_callback) and def image_callback(msg1, msg2). Set cnt = msg1.header.stamp inside the callback.
  • By the way, it seems that you are writing to inaccessible locations, i.e. /home/test_ and /home/ori_time_.
ravijoshi gravatar image ravijoshi  ( 2022-09-22 01:12:26 -0600 )edit