Skip to content

Commit

Permalink
Fix wait_for_future for cancelled futures (#88)
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
mhidalgo-bdai authored Apr 11, 2024
1 parent 25ba838 commit 679b2f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bdai_ros2_wrappers/bdai_ros2_wrappers/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ def wait_for_future(future: Future, timeout_sec: Optional[float] = None, *, cont
event = Event()
context.on_shutdown(event.set)
future.add_done_callback(lambda _: event.set())
if future.cancelled():
event.set()
event.wait(timeout=timeout_sec)
return future.done()
15 changes: 15 additions & 0 deletions bdai_ros2_wrappers/test/test_futures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2024 Boston Dynamics AI Institute Inc. All rights reserved.

from rclpy.task import Future

from bdai_ros2_wrappers.futures import wait_for_future
from bdai_ros2_wrappers.scope import ROSAwareScope


def test_wait_for_cancelled_future(ros: ROSAwareScope) -> None:
"""Asserts that waiting for a cancelled future does not hang indefinitely."""
future = Future()
future.cancel()

assert not wait_for_future(future, context=ros.context)
assert future.cancelled()

0 comments on commit 679b2f4

Please sign in to comment.