Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(async_utils): only close event loop after worker thread stops#3348

Open
andrewwhitecdw wants to merge 2 commits into
NVIDIA-NeMo:mainNVIDIA-NeMo/RL:mainfrom
andrewwhitecdw:fix-async-loop-thread-close-raceandrewwhitecdw/RL:fix-async-loop-thread-close-raceCopy head branch name to clipboard
Open

fix(async_utils): only close event loop after worker thread stops#3348
andrewwhitecdw wants to merge 2 commits into
NVIDIA-NeMo:mainNVIDIA-NeMo/RL:mainfrom
andrewwhitecdw:fix-async-loop-thread-close-raceandrewwhitecdw/RL:fix-async-loop-thread-close-raceCopy head branch name to clipboard

Conversation

@andrewwhitecdw

Copy link
Copy Markdown

Problem

AsyncLoopThread.close() calls loop.close() unconditionally after joining the worker thread, even if join() times out and the worker thread is still running. Closing a loop that is still executing in another thread can raise RuntimeError: Cannot close a running event loop and leaves the worker thread in an undefined state.

Fix

Only call loop.close() when the worker thread has actually stopped (not self._thread.is_alive()). If the thread is still alive after join(timeout), we leave the loop open so the thread can finish stopping it safely.

Verification

The existing regression tests in tests/test_async_utils.py already assert this exact behavior:

  • test_close_does_not_close_loop_while_worker_thread_is_alive
  • test_close_closes_loop_when_worker_thread_is_dead
  • test_close_is_idempotent_when_loop_already_closed

This change makes the implementation match those regression expectations.

@copy-pr-bot

copy-pr-bot Bot commented Jul 25, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@andrewwhitecdw
andrewwhitecdw marked this pull request as ready for review July 26, 2026 03:48
@andrewwhitecdw
andrewwhitecdw requested a review from a team as a code owner July 26, 2026 03:48
`AsyncLoopThread.close()` could call `loop.close()` while the worker
thread was still alive if `join()` timed out, which would later cause
`RuntimeError: Cannot close a running event loop` when the thread
tried to stop the loop. Close the loop only when the worker thread has
actually stopped.

Signed-off-by: Andrew White <andrewh@cdw.com>
@andrewwhitecdw
andrewwhitecdw force-pushed the fix-async-loop-thread-close-race branch from 82716a3 to a49fa68 Compare July 26, 2026 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.