Repository navigation
[Serve] Set RAY_SERVE_RUN_SYNC_IN_THREADPOOL=1 to run sync in threadpool by default - #61229
abrarsheikh merged 13 commits into
Conversation
Signed-off-by: Peter Nguyen <[email protected]>
Signed-off-by: Peter Nguyen <[email protected]>
Signed-off-by: Peter Nguyen <[email protected]>
Signed-off-by: Peter Nguyen <[email protected]>
Signed-off-by: Peter Nguyen <[email protected]>
There was a problem hiding this comment.
Code Review
The pull request updates the default behavior of RAY_SERVE_RUN_SYNC_IN_THREADPOOL to 1, meaning synchronous methods will now run in a threadpool by default. This change improves event loop responsiveness. The documentation and internal warnings have been updated to reflect this new default and to provide guidance on thread safety and how to opt out of the new behavior. The changes are well-documented and address the issue of blocking async operations.
|
For some reason, Gemini just left a ton of review comments explaining each of my changes. None of them suggested any changes, so I just resolved them all. |
Signed-off-by: Peter Nguyen <[email protected]>
| - The event loop is free to keep serving other requests while sync methods run. | ||
|
|
||
| You can opt in to the future behavior now by setting: | ||
| Ensure your handlers and shared state are thread-safe. If you opt out by setting: |
There was a problem hiding this comment.
Good to have a code example here that shows what non-thread safe looks like.
There was a problem hiding this comment.
| warnings.warn( | ||
| RAY_SERVE_RUN_SYNC_IN_THREADPOOL_THREAD_SAFETY_WARNING.format( | ||
| method_name=callable.__name__, |
There was a problem hiding this comment.
please do a local run where this log line is seen, want to make sure the log is not repeated multiple times.
| ): | ||
| self._warned_about_sync_method_change = True | ||
| self._warned_about_sync_method_event_loop = True | ||
| warnings.warn( |
There was a problem hiding this comment.
please do a local run where this log line is seen. Add it to the PR description.
There was a problem hiding this comment.
done and added to PR description for both warning messages
Co-authored-by: Abrar Sheikh <[email protected]> Signed-off-by: Peter Nguyen <[email protected]>
Signed-off-by: Peter Nguyen <[email protected]>
…ncio best practices doc Signed-off-by: Peter Nguyen <[email protected]>
Signed-off-by: Peter Nguyen <[email protected]>
| # __ray_parallel_end__ | ||
|
|
||
|
|
||
| if __name__ == "__main__": |
There was a problem hiding this comment.
please include the newly added examples under here so that they run as part of CI.
Signed-off-by: Peter Nguyen <[email protected]>
| RAY_SERVE_RUN_SYNC_IN_THREADPOOL_THREAD_SAFETY_WARNING.format( | ||
| method_name=callable.__name__, | ||
| ) | ||
| ) |
There was a problem hiding this comment.
Signed-off-by: Peter Nguyen <[email protected]>
|
try merging master again, I think that will fix it |
Signed-off-by: Peter Nguyen <[email protected]>
… threadpool by default (ray-project#61229)" This reverts commit 9e1fa2e. Signed-off-by: akyang-anyscale <[email protected]>
#61369) … threadpool by default (#61229)" This reverts commit 9e1fa2e. Enabling the flag by default introduces a significant perf regression for lightweight workloads that are sensitive to context switching and that do not take full advantage of offloading work from python. will post a RFC for community feedback before making this the default > Thank you for contributing to Ray! 🚀 > Please review the [Ray Contribution Guide](https://docs.ray.io/en/master/ray-contribute/getting-involved.html) before opening a pull request. >⚠️ Remove these instructions before submitting your PR. > 💡 Tip: Mark as draft if you want early feedback, or ready for review when it's complete. ## Description > Briefly describe what this PR accomplishes and why it's needed. ## Related issues > Link related issues: "Fixes #1234", "Closes #1234", or "Related to #1234". ## Additional information > Optional: Add implementation details, API changes, usage examples, screenshots, etc. Signed-off-by: akyang-anyscale <[email protected]>
## Description Run synchronous deployment methods in a thread pool by default so blocking synchronous I/O does not block the replica event loop. `RAY_SERVE_RUN_SYNC_IN_THREADPOOL=0` retains the legacy behavior for deployments whose shared state is not thread-safe. | Environment variable | `master` | After this PR | | --- | --- | --- | | `RAY_SERVE_RUN_SYNC_IN_THREADPOOL` | OFF | ON | The opt-in warning has been emitted since [#48897](#48897) merged on November 26, 2024—667 days (about 22 months) before this default swap. This provided an extended migration period for users to make synchronous deployment code thread-safe or convert it to `async def`. The documentation now describes thread safety requirements and the opt-out. This default was previously enabled in [#61229](#61229) and reverted in [#61369](#61369). ## Related PRs These default changes are independent and can merge in any order: - [#66432 Enable HAProxy by default on Linux](#66432) - [#66434 Select request body forwarding by router](#66434) - [#66435 Apply Serve LLM replica performance defaults](#66435) ## Tests - `RAY_USE_UVLOOP=0 python -m pytest -q python/ray/serve/tests/test_replica_sync_methods.py`: 9 passed. The executor-introspection cases target the standard asyncio loop used by this Bazel test. - Updated the thread-pool test target description - Pre-commit, including Ruff, Black, Buildifier, mypy, Pyrefly, docs style, and import-order checks ## Duplicate search I searched open Ray PRs for this environment variable and found no active duplicate. ## AI assistance AI assistance was used for implementation and testing. The generated changes and test results require submitter review before the PR is marked ready. Signed-off-by: Seiji Eicher <[email protected]>
## Description Run synchronous deployment methods in a thread pool by default so blocking synchronous I/O does not block the replica event loop. `RAY_SERVE_RUN_SYNC_IN_THREADPOOL=0` retains the legacy behavior for deployments whose shared state is not thread-safe. | Environment variable | `master` | After this PR | | --- | --- | --- | | `RAY_SERVE_RUN_SYNC_IN_THREADPOOL` | OFF | ON | The opt-in warning has been emitted since [ray-project#48897](ray-project#48897) merged on November 26, 2024—667 days (about 22 months) before this default swap. This provided an extended migration period for users to make synchronous deployment code thread-safe or convert it to `async def`. The documentation now describes thread safety requirements and the opt-out. This default was previously enabled in [ray-project#61229](ray-project#61229) and reverted in [ray-project#61369](ray-project#61369). ## Related PRs These default changes are independent and can merge in any order: - [ray-project#66432 Enable HAProxy by default on Linux](ray-project#66432) - [ray-project#66434 Select request body forwarding by router](ray-project#66434) - [ray-project#66435 Apply Serve LLM replica performance defaults](ray-project#66435) ## Tests - `RAY_USE_UVLOOP=0 python -m pytest -q python/ray/serve/tests/test_replica_sync_methods.py`: 9 passed. The executor-introspection cases target the standard asyncio loop used by this Bazel test. - Updated the thread-pool test target description - Pre-commit, including Ruff, Black, Buildifier, mypy, Pyrefly, docs style, and import-order checks ## Duplicate search I searched open Ray PRs for this environment variable and found no active duplicate. ## AI assistance AI assistance was used for implementation and testing. The generated changes and test results require submitter review before the PR is marked ready. Signed-off-by: Seiji Eicher <[email protected]>



Description
Set
RAY_SERVE_RUN_SYNC_IN_THREADPOOL=1to run sync in threadpool by defaultRelated issues
Fixes #61225
Additional information
As requested in the issue, I've added a one-time warning that explains method calls should be thread-safe or use
async definstead. Additionally, there was an existing warning that the configuration would eventually be changed to default to 1. I've modified that warning to instead say that the method blocks async operations, and they can instead run it in the thread pool by setting the configuration to 1.I ran the following script (toggling the environment variable), and verified that the corresponding warning message only appears once for both cases.
Script and resulting output
Output for RAY_SERVE_RUN_SYNC_IN_THREADPOOL=0
Output for RAY_SERVE_RUN_SYNC_IN_THREADPOOL=1