fix: rebind asyncio Semaphore and HTTP client on event-loop change#1858
Open
leofan-lab wants to merge 1 commit intoTHUDM:mainfrom
Open
fix: rebind asyncio Semaphore and HTTP client on event-loop change#1858leofan-lab wants to merge 1 commit intoTHUDM:mainfrom
leofan-lab wants to merge 1 commit intoTHUDM:mainfrom
Conversation
asyncio primitives (Semaphore, httpx.AsyncClient's internal locks / pool) bind to the loop they were created on. Ray actors can serve calls on a different loop across re-entries (e.g. rollout → eval), and reusing a loop-bound primitive from a new loop raises "attached to a different event loop" or silently stalls. Make both GenerateState.semaphore and _get_http_client() lazily (re)build when the current loop is not the one the cached primitive was bound to. Loop identity is tracked via weakref so a recycled id() on a fresh loop doesn't look like the old one. The old HTTP client is dropped rather than aclose()'d from a foreign loop (which races on its own pool locks); sockets close when GC collects it. Tests: tests/test_http_utils_loop_rebind.py covers the rebind firing across two asyncio.run() calls and the no-op within a single loop. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Ray actors can serve calls on different asyncio event loops across re-entries.
asyncio.Semaphoreandhttpx.AsyncClient's internal pool locks bind to the loop that created them; using them from a different loop later raisesRuntimeError: ... is attached to a different event loopor silently stalls.Make
GenerateState.semaphoreand_get_http_client()lazily rebuild when the running loop differs from the one the cached primitive was bound to. Loop identity is tracked viaweakref.ref(loop)rather thanid(loop)to avoid address recycling.When this fires
Encountered while wiring up in-training eval for async rollouts. The first eval call after a rollout crashed at
async with state.semaphore:— the rollout had bound the Semaphore to loop A, and Ray re-entered the actor on loop B for eval.Tests
tests/test_http_utils_loop_rebind.py— tests covering rebind acrossasyncio.run()calls and same-loop reuse. Mutation-tested: breaking the rebind condition causestest_http_client_rebinds_across_fresh_event_loopsto fail with a clear assertion message.