Why do I get deadlock when setting a future in a subscription callback? (Only with MultiThreadedExecutor) (rclpy)
I have a node that publishes message, and another node (constructed with an unset future) that sets the result of the future to the incoming message in its subscription callback. I then add both of these nodes to an executor (in the same process) and spin until the future is complete.
If I am using a MultiThreadedExecutor
then the future gets set, but it gets stuck during a spin
and the program never exits. If I am using a SingleThreadedExecutor
then this does not happen, and the program successfully exits.
My Publisher Node looks like this:
pubnode = Node('pubnode_' + str(os.getpid()))
pub1 = pubnode.create_publisher(String, 'topic1', latching_qos)
msg1 = String()
msg1.data = "hello1"
pubnode.get_logger().info("Publishing hello1")
pub1.publish(msg1)
My Subscription Node looks like this:
future_msgs = Future()
subnode = Node('subnode_' + str(os.getpid()))
subnode.create_subscription(String, 'topic1', lambda msg : ([
subnode.get_logger().info("Received message on topic1"),
future_msgs.set_result(msg)
]), latching_qos)
And I start the Nodes like this:
exe = MultiThreadedExecutor() if args.exe == 'M' else SingleThreadedExecutor()
print("Using {}".format(str(type(exe))))
exe.add_node(pubnode)
exe.add_node(subnode)
future_msgs.add_done_callback(lambda fut : print("Future is done"))
exe.spin_until_future_complete(future_msgs)
print("Goodbye!")
I have also tried:
while future_msgs.done() is False:
print('------ Begin Spin')
exe.spin_once()
print('------ End Spin')
But I have the same issue. The ------ Begin Spin
and Received message on topic1
are printed but during the second cycle a '------ Begin Spin
message is printed without a following ------ End Spin
, so it must be stuck inside spin_once()
.
The full code can be found here, with instructions on how to run it here.
Why does this happen? Or is this a bug in ros? I will submit an issue if this is a problem with ros, and not just how I am using it. If that is the case, where should I submit the bug report?
Thanks.
Edit:
I have submitted a bug report here