Skip to content

[serve] Add FF to run sync methods in a threadpool - #48897

Merged
zcin merged 14 commits into
ray-project:masterfrom
edoakes:eoakes/sync-threadpool
Nov 26, 2024
Merged

zcin merged 14 commits into
ray-project:masterfrom
edoakes:eoakes/sync-threadpool

Conversation

@edoakes

@edoakes edoakes commented Nov 23, 2024 •

Copy link
Copy Markdown
Collaborator

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:

  • Get it working for sync generators.
  • Add warning for default change (people can keep behavior by changing to async def).
  • Add/update UserCallableWrapper tests.
  • Add/update some integration tests (verify that request context is set correctly!).
  • Set maximum thread pool size.

Related issue number

Closes #44354
Closes #44403
Closes #48903

Checks

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 edoakes added the go add ONLY when ready to merge, run all tests label Nov 23, 2024
@edoakes
edoakes requested a review from zcin November 23, 2024 15:45
Signed-off-by: Edward Oakes <[email protected]>

@zcin zcin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@zcin
zcin merged commit 0f2c62c into ray-project:master Nov 26, 2024
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-backlog go add ONLY when ready to merge, run all tests

Projects

None yet

3 participants