Skip to content

[core] Patch grpc with RAY_num_grpc_threads to control grpc thread count - #54988

Merged
jjyao merged 28 commits into
ray-project:masterfrom
ZacAttack:grpc_patch
Aug 7, 2025
Merged

jjyao merged 28 commits into
ray-project:masterfrom
ZacAttack:grpc_patch

Conversation

@ZacAttack

@ZacAttack ZacAttack commented Jul 28, 2025 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

Workers spawn extraneous threads that aren't necessary. It's been identified that a major source of thread bloat is coming from the use of grpc in the worker processes. As an incremental step towards a full resolution, this patch adds "RAY_worker_num_grpc_internal_threads" as an environment variable which is honored by the raylet when spawning workers. Setting this environment variable in your base image or in your code will limit the number of threads spawned by grpc in the worker.

An example usage of this api would look like:

import ray
from ray.util.queue import Queue as RayQueue
from ray.runtime_env import RuntimeEnv

# restrict the number of threads for grpc to 1 per worker (this is probably what you want to do)
# be sure this is set before the any call to ray.init or ray.remote
os.environ['RAY_worker_num_grpc_internal_threads'] = '1'

class SimpleActor:
    def __init__(self):
        ....

Related issue number

Closes #36936
Closes #54225

Checks

@ZacAttack ZacAttack added the go add ONLY when ready to merge, run all tests label Jul 28, 2025

@gemini-code-assist gemini-code-assist Bot 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.

Summary of Changes

Hello @ZacAttack, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

I've implemented a core change to address thread bloat in Ray worker processes, specifically targeting gRPC's thread usage. This change allows users to explicitly control the number of gRPC threads by setting a new environment variable, RAY_num_grpc_threads, via Ray's RuntimeEnv. This provides an incremental step towards optimizing resource utilization and reducing unnecessary thread overhead in worker processes.

Highlights

  • gRPC Thread Control: I've introduced a new mechanism to control the number of gRPC threads spawned by Ray workers. This is achieved by applying a custom patch to the gRPC library during the build process.
  • RAY_num_grpc_threads Environment Variable: This pull request enables the use of the RAY_num_grpc_threads environment variable. When this variable is set in the RuntimeEnv for a Ray worker, gRPC will use the specified value as its thread pool size. This allows for fine-grained control over thread resource consumption.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@ZacAttack
ZacAttack requested a review from a team July 28, 2025 22:22
@ZacAttack ZacAttack added the core Issues that should be addressed in Ray Core label Jul 28, 2025
Comment thread bazel/ray_deps_setup.bzl Resolved
Comment thread python/ray/tests/test_basic.py Resolved
Comment thread python/ray/tests/test_basic.py Resolved
Comment thread python/ray/tests/test_basic.py Resolved
Comment thread python/ray/tests/test_basic.py Resolved
Comment thread python/ray/tests/test_basic.py Resolved
@ZacAttack
ZacAttack requested a review from a team as a code owner August 4, 2025 21:13
Comment thread src/ray/common/ray_config_def.h Resolved
Co-authored-by: Jiajun Yao <[email protected]>
Signed-off-by: Zac Policzer <[email protected]>

@edoakes edoakes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: let's name the patch something like grpc-configurable-thread-count

Comment thread src/ray/common/ray_config_def.h Resolved

@edoakes edoakes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One nit, a merge conflict, then LGTM

@edoakes

edoakes commented Aug 6, 2025

Copy link
Copy Markdown
Collaborator

@ZacAttack I fixed merge conflict but please fix DCO (sign off commits)

@edoakes
edoakes enabled auto-merge (squash) August 6, 2025 22:30

@israbbani israbbani 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.

🚢 nicely done!

Comment thread python/ray/tests/test_basic.py Resolved
@github-actions
github-actions Bot disabled auto-merge August 6, 2025 23:55
@jjyao
jjyao merged commit a4910fb into ray-project:master Aug 7, 2025
dioptre pushed a commit to sourcetable/ray that referenced this pull request Aug 20, 2025
…unt (ray-project#54988)

Signed-off-by: Zac Policzer <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: zac <[email protected]>
Co-authored-by: Jiajun Yao <[email protected]>
Co-authored-by: Edward Oakes <[email protected]>
Signed-off-by: Andrew Grosser <[email protected]>
jugalshah291 pushed a commit to jugalshah291/ray_fork that referenced this pull request Sep 11, 2025
…unt (ray-project#54988)

Signed-off-by: Zac Policzer <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: zac <[email protected]>
Co-authored-by: Jiajun Yao <[email protected]>
Co-authored-by: Edward Oakes <[email protected]>
Signed-off-by: jugalshah291 <[email protected]>
dstrodtman pushed a commit that referenced this pull request Oct 6, 2025
…unt (#54988)

Signed-off-by: Zac Policzer <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
Signed-off-by: zac <[email protected]>
Co-authored-by: Jiajun Yao <[email protected]>
Co-authored-by: Edward Oakes <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@morotti

morotti commented Oct 31, 2025

Copy link
Copy Markdown
Contributor

@ZacAttack @israbbani @edoakes

Quick notice that the grpc code you patched is wrong (see thread linked above)
I have opened the bug to grpc, they need to correct their code to detect the number of CPU correctly.

  • You probably want to replace _SC_NPROCESSORS_CONF with _SC_NPROCESSORS_ONLN in your patch, trivial fix.
  • Then the code needs to detect cpu limits, that's a bit more work.

@ZacAttack

Copy link
Copy Markdown
Contributor Author

@ZacAttack @israbbani @edoakes

Quick notice that the grpc code you patched is wrong (see thread linked above)
I have opened the bug to grpc, they need to correct their code to detect the number of CPU correctly.

  • You probably want to replace _SC_NPROCESSORS_CONF with _SC_NPROCESSORS_ONLN in your patch, trivial fix.
  • Then the code needs to detect cpu limits, that's a bit more work.

@morotti thanks for the tip. Out of curiosity, what is the difference between these variables? We've seen the patch as is work (or at least, work to the desired goal of reducing thread count). What will be the observed difference with this other variable?

@morotti

morotti commented Oct 31, 2025 •

Copy link
Copy Markdown
Contributor

@ZacAttack see linked thread

I have a development machine with 48 cores but grpc is seeing 128 cores, which is the number of cores on the hypervisor.

sysconf(_SC_NPROCESSORS_CONF) is returning the number of CPU on the host, instead of the number of CPU available to the VM.
I think you need to use _SC_NPROCESSORS_ONLN instead.

output from the man example: https://man7.org/linux/man-pages/man3/get_nprocs.3.html
"This system has 128 processors configured and 48 processors available."

   #include <stdio.h>
   #include <stdlib.h>
   #include <sys/sysinfo.h>
   int
   main(void)
   {
       printf("This system has %d processors configured and "
               "%d processors available.\n",
               get_nprocs_conf(), get_nprocs());
       exit(EXIT_SUCCESS);
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Core] thread creation error, even with environment variables all set to 1 [Core] Too many threads in ray worker

7 participants