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

Why do I get deadlock when setting a future in a subscription callback? (Only with MultiThreadedExecutor) (rclpy)

asked 2020-06-19 09:16:21 -0600

CraigH92 gravatar image

updated 2020-06-19 09:44:35 -0600

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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-02-17 08:44:05 -0600

130s gravatar image

Looks like this was identified as a bug and already resolved in the mainline development in rclpy#585, which is the OP reported. Nice community-driven dev cycle!

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-06-19 09:16:21 -0600

Seen: 1,202 times

Last updated: Feb 17 '23