Repository navigation
[serve] Add FF to run sync methods in a threadpool - #48897
Merged
Merged
Conversation
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
edoakes
force-pushed
the
eoakes/sync-threadpool
branch
from
November 23, 2024 15:56
a581526 to
eee4140
Compare
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
jecsand838
pushed a commit
to jecsand838/ray
that referenced
this pull request
Dec 4, 2024
## Why are these changes needed? Adds a feature flag to run sync user-defined methods in a threadpool by default. This matches the existing behavior when using a FastAPI ingress. This should address a lot of user confusion and make it easier to write performant code by default. For example, just sticking a torch model call in a sync method will now provide reasonable performance out of the box. However, there may be some existing user code that is not thread safe, so we need to do a gentle migration. This PR introduces the behavior behind a feature flag and warns users about the upcoming change and how to opt into the new behavior or maintain existing behavior once it does (just adding `async def` will do it). I've opted to set the max thread pool size to `max_ongoing_requests`, which seems like a reasonable policy. If needed we can add a user-facing API for this in the future. TODO before merging: - [x] Get it working for sync generators. - [x] Add warning for default change (people can keep behavior by changing to async def). - [x] Add/update UserCallableWrapper tests. - [x] Add/update some integration tests (verify that request context is set correctly!). - [x] Set maximum thread pool size. ## Related issue number Closes ray-project#44354 Closes ray-project#44403 Closes ray-project#48903 --------- Signed-off-by: Edward Oakes <[email protected]> Signed-off-by: Connor Sanders <[email protected]>
eicherseiji
added a commit
that referenced
this pull request
Sep 29, 2026
## 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]>
nadongjun
pushed a commit
to nadongjun/ray
that referenced
this pull request
Oct 6, 2026
## 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]>
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?
Adds a feature flag to run sync user-defined methods in a threadpool by default. This matches the existing behavior when using a FastAPI ingress.
This should address a lot of user confusion and make it easier to write performant code by default. For example, just sticking a torch model call in a sync method will now provide reasonable performance out of the box.
However, there may be some existing user code that is not thread safe, so we need to do a gentle migration. This PR introduces the behavior behind a feature flag and warns users about the upcoming change and how to opt into the new behavior or maintain existing behavior once it does (just adding
async defwill do it).I've opted to set the max thread pool size to
max_ongoing_requests, which seems like a reasonable policy. If needed we can add a user-facing API for this in the future.TODO before merging:
Related issue number
Closes #44354
Closes #44403
Closes #48903
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.