[Serve] Call shared long poll client router registration in event loop#53613
Merged
zcin merged 3 commits intoray-project:masterfrom Jun 13, 2025
Merged
Conversation
Signed-off-by: Josh Karpel <josh.karpel@gmail.com>
Contributor
Author
|
cc @zcin for review, we found another one! |
Contributor
There was a problem hiding this comment.
Pull Request Overview
Updates the shared long-poll client to schedule router registrations on the provided asyncio event loop, preventing concurrent mutations of the router set.
- Stores the event loop in
SharedRouterLongPollClient - Switches
LongPollClientto use the stored loop - Wraps
registercalls withcall_soon_threadsafeand introduces a private_registermethod
Comments suppressed due to low confidence (3)
python/ray/serve/_private/router.py:904
- Add unit tests that simulate concurrent long-poll callbacks and registrations to verify that
call_soon_threadsafepreventsRuntimeErrorduring iteration.
self.event_loop.call_soon_threadsafe(self._register, router)
python/ray/serve/_private/router.py:906
- [nitpick] Add a docstring for
_registerexplaining that it runs on the event loop to safely mutateself.routers.
def _register(self, router: AsyncioRouter) -> None:
python/ray/serve/_private/router.py:904
- Consider applying the same
call_soon_threadsafescheduling to any deregistration or cleanup paths to avoid similar race conditions on removal.
self.event_loop.call_soon_threadsafe(self._register, router)
Contributor
Author
zcin
approved these changes
Jun 12, 2025
Contributor
Author
|
@zcin looks like CI passed, I think this is ready to merge 🤞🏻 |
Contributor
Author
|
Thanks! |
elliot-barn
pushed a commit
that referenced
this pull request
Jun 18, 2025
#53613) ## Why are these changes needed? We found a second bug with the shared long poll client, similar to #52793 - this time it's for a different part of the registration process. The error we saw looked like: ``` 2025-06-06 14:29:14 ERROR Exception in callback LongPollClient._process_update.<locals>.chained() at /app/virtualenv/lib64/python3.12/site-packages/ray/serve/_private/long_poll.py:215 handle: <Handle LongPollClient._process_update.<locals>.chained() at /app/virtualenv/lib64/python3.12/site-packages/ray/serve/_private/long_poll.py:215> Traceback (most recent call last): File "/usr/lib64/python3.12/asyncio/events.py", line 88, in _run self._context.run(self._callback, *self._args) File "/app/virtualenv/lib64/python3.12/site-packages/ray/serve/_private/long_poll.py", line 216, in chained callback(arg) File "/app/virtualenv/lib64/python3.12/site-packages/ray/serve/_private/router.py", line 833, in update_deployment_config for router in self.routers[deployment_id]: ~~~~~~~~~~~~^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/_weakrefset.py", line 65, in __iter__ for itemref in self.data: ^^^^^^^^^ RuntimeError: Set changed size during iteration ``` This is getting pretty gnarly. I feel like there's a potential future refactor of the long poll client code that makes it truly `async` so that we don't need to worry about this stuff? But I'm not too familiar with how/why the background threads are being used here. ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [ ] I've included any doc changes needed for https://docs.ray.io/en/master/. - [ ] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [ ] Unit tests - [ ] Release tests - [x] This PR is not tested :( --------- Signed-off-by: Josh Karpel <josh.karpel@gmail.com> Signed-off-by: elliot-barn <elliot.barnwell@anyscale.com>
elliot-barn
pushed a commit
that referenced
this pull request
Jul 2, 2025
#53613) ## Why are these changes needed? We found a second bug with the shared long poll client, similar to #52793 - this time it's for a different part of the registration process. The error we saw looked like: ``` 2025-06-06 14:29:14 ERROR Exception in callback LongPollClient._process_update.<locals>.chained() at /app/virtualenv/lib64/python3.12/site-packages/ray/serve/_private/long_poll.py:215 handle: <Handle LongPollClient._process_update.<locals>.chained() at /app/virtualenv/lib64/python3.12/site-packages/ray/serve/_private/long_poll.py:215> Traceback (most recent call last): File "/usr/lib64/python3.12/asyncio/events.py", line 88, in _run self._context.run(self._callback, *self._args) File "/app/virtualenv/lib64/python3.12/site-packages/ray/serve/_private/long_poll.py", line 216, in chained callback(arg) File "/app/virtualenv/lib64/python3.12/site-packages/ray/serve/_private/router.py", line 833, in update_deployment_config for router in self.routers[deployment_id]: ~~~~~~~~~~~~^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/_weakrefset.py", line 65, in __iter__ for itemref in self.data: ^^^^^^^^^ RuntimeError: Set changed size during iteration ``` This is getting pretty gnarly. I feel like there's a potential future refactor of the long poll client code that makes it truly `async` so that we don't need to worry about this stuff? But I'm not too familiar with how/why the background threads are being used here. ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [ ] I've included any doc changes needed for https://docs.ray.io/en/master/. - [ ] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [ ] Unit tests - [ ] Release tests - [x] This PR is not tested :( --------- Signed-off-by: Josh Karpel <josh.karpel@gmail.com> Signed-off-by: elliot-barn <elliot.barnwell@anyscale.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.
Why are these changes needed?
We found a second bug with the shared long poll client, similar to #52793 - this time it's for a different part of the registration process. The error we saw looked like:
This is getting pretty gnarly. I feel like there's a potential future refactor of the long poll client code that makes it truly
asyncso that we don't need to worry about this stuff? But I'm not too familiar with how/why the background threads are being used here.Related issue number
Checks
git commit -s) in this PR.scripts/format.shto lint the changes in this PR.method in Tune, I've added it in
doc/source/tune/api/under thecorresponding
.rstfile.