Skip to content

Commit f09fcec

Browse files
committed
Added lock to protect futures for multithreaded executor
1 parent 4859c8a commit f09fcec

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

rclpy/rclpy/executors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ def __init__(
982982
'Use the SingleThreadedExecutor instead.')
983983
self._futures: List[Future[Any]] = []
984984
self._executor = ThreadPoolExecutor(num_threads)
985+
self._futures_lock = Lock()
985986

986987
def _spin_once_impl(
987988
self,
@@ -1002,12 +1003,11 @@ def _spin_once_impl(
10021003
else:
10031004
self._executor.submit(handler)
10041005
self._futures.append(handler)
1005-
# make a copy of the list that we iterate over while modifying it
1006-
# (https://stackoverflow.com/q/1207406/3753684)
1007-
for future in self._futures[:]:
1008-
if future.done():
1009-
self._futures.remove(future)
1010-
future.result() # raise any exceptions
1006+
with self._futures_lock:
1007+
for future in self._futures:
1008+
if future.done():
1009+
self._futures.remove(future)
1010+
future.result() # raise any exceptions
10111011

10121012
def spin_once(self, timeout_sec: Optional[float] = None) -> None:
10131013
self._spin_once_impl(timeout_sec)

0 commit comments

Comments
 (0)