WaitForTopics blocks interpreter exit if shutdown() is never called

Open
#592 1 comment 0 reactions 0 assignees View on GitHub

@aadilkhan085529 is already working on this.

Since Nov 11, 2026.

  • #596 by @aadilkhan085529 — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
53/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend, testing

Research direction

Start by reading WaitForTopics in launch_testing_ros/wait_for_topics.py, especially __init__, _spin_handle_external_shutdown, shutdown(), and the context manager. Check how the thread and rclpy executor behave during Python interpreter finalization, then choose and validate a cleanup approach against the documented manual wait()/shutdown() usage. Done means a failed wait() cannot leave a thread that prevents pytest from exiting, without causing a crash during shutdown.

Written by the indexing model from the issue text.

Description

Description

WaitForTopics.__init__ starts a non-daemon thread that spins the executor, and only WaitForTopics.shutdown() stops it. If a caller never reaches shutdown() (most commonly because an assertion between wait() and shutdown() fails), the thread keeps spinning after the test finishes. The interpreter then blocks forever in threading._shutdown waiting for it.

Under pytest this happens after the session has finished, so pytest-timeout can't catch it. The process just never exits, and in CI that means the build runs until the global job timeout (600 minutes on ci.ros2.org).

This is the documented "Method 2" usage pattern:

wait_for_topics = WaitForTopics(topic_list, timeout=5.0)
assert wait_for_topics.wait()   # if this fails, shutdown() never runs
...
wait_for_topics.shutdown()

Evidence

Found while chasing launch_testing_ros hangs on ci.ros2.org (e.g. nightly_linux-aarch64_debug #3744). With the launch tests repeated 51x on Debug builds (ci_launcher 20974), the faulthandler watchdog from #591 dumped this at exit on Linux, aarch64 and RHEL. There was one such thread per failed wait_for_topic_launch_test repeat (2, 1 and 6 respectively):

Thread 0x... [Thread-526 (_sp] (most recent call first):
  File ".../rclpy/executors.py", line 1011 in _wait_for_ready_callbacks
  ...
  File ".../rclpy/executors.py", line 518 in spin
  File ".../launch_testing_ros/wait_for_topics.py", line 94 in _spin_handle_external_shutdown
  ...
Thread 0x... [python3] (most recent call first):
  File "/usr/lib/python3.14/threading.py", line 1583 in _shutdown

#591 fixes launch_testing_ros's own test (shutdown() in finally, longer timeouts), but any user test that uses WaitForTopics the same way can still hang its CI job.

Possible fixes

  • Make the spin thread a daemon (daemon=True). This is the simplest. The thread is normally blocked in rcl_wait with the GIL released, so the interpreter can exit past it. We need to check that rclpy doesn't crash at finalization with a daemon thread inside spin() and an un-shut-down context.
  • Shut down before Python waits for threads. Register shutdown() to run before the interpreter joins non-daemon threads. threading._register_atexit does exactly that, but it's private API. Plain atexit runs too late, after the join.
  • In either case, consider having wait() / __enter__ document or enforce cleanup. For example, encourage the context-manager form, which already calls shutdown() on failure, over manual wait()/shutdown() in the docstring.
Dominant language
Python
Stars
84
Forks
115
Avg merge
2d 1h
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ros2/launch_ros

All issues in ros2/launch_ros

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.