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#3348andrewwhitecdw wants to merge 2 commits intoNVIDIA-NeMo:mainNVIDIA-NeMo/RL:mainfrom andrewwhitecdw:fix-async-loop-thread-close-raceandrewwhitecdw/RL:fix-async-loop-thread-close-raceCopy head branch name to clipboard
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
`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
force-pushed
the
fix-async-loop-thread-close-race
branch
from
July 26, 2026 03:52
82716a3 to
a49fa68
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
AsyncLoopThread.close()callsloop.close()unconditionally after joining the worker thread, even ifjoin()times out and the worker thread is still running. Closing a loop that is still executing in another thread can raiseRuntimeError: Cannot close a running event loopand 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 afterjoin(timeout), we leave the loop open so the thread can finish stopping it safely.Verification
The existing regression tests in
tests/test_async_utils.pyalready assert this exact behavior:test_close_does_not_close_loop_while_worker_thread_is_alivetest_close_closes_loop_when_worker_thread_is_deadtest_close_is_idempotent_when_loop_already_closedThis change makes the implementation match those regression expectations.