Skip to content

[bugfix][critical] critical fix: correctly detect the number of CPU - #59041

Closed
morotti wants to merge 2 commits into
ray-project:masterfrom
morotti:patch-2
Closed

morotti wants to merge 2 commits into
ray-project:masterfrom
morotti:patch-2

Conversation

@morotti

@morotti morotti commented Nov 27, 2025

Copy link
Copy Markdown
Contributor

Hello,

On a LXC node with 8 CPU.

  • _SC_NPROCESSORS_ONLN gives 8 CPU. the correct number.
  • _SC_NPROCESSORS_CONF gives 256 CPU, which is the hardware on the hypervisor. this is not correct and cannot be used by the application.

This bug keeps crashing my ray cluster out of memory because each ray worker is trying to create large number of threads and running out of memory.

see discussion in #54988
I've raised the patch upstream too. grpc/grpc#41144

Regards.

on a LXC node with 8 CPU.

_SC_NPROCESSORS_ONLN gives 8 CPU. the correct number.
_SC_NPROCESSORS_CONF gives 256 CPU, which is the hardware on the hypervisor. this is not correct and cannot be used by the application.

Signed-off-by: morotti <[email protected]>

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

Code Review

This pull request provides a critical fix for correctly detecting the number of CPUs in containerized environments like LXC. By changing sysconf(_SC_NPROCESSORS_CONF) to sysconf(_SC_NPROCESSORS_ONLN), it ensures that the application uses the number of available online processors rather than the total configured processors of the host machine. This correctly resolves the out-of-memory issues described. The change is accurate and well-justified.

@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Nov 27, 2025
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Dec 12, 2025
@morotti

morotti commented Dec 12, 2025

Copy link
Copy Markdown
Contributor Author

please review the pull request, it's a simple fix

@github-actions github-actions Bot added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Dec 12, 2025
@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Dec 12, 2025
@Yicheng-Lu-llll

Copy link
Copy Markdown
Member

I’m not able to reproduce this on my devbox. However, according to the man page https://man7.org/linux/man-pages/man3/get_nprocs.3.html this behavior makes sense:

   The function get_nprocs_conf() returns the number of processors
   configured by the operating system.

   The function get_nprocs() returns the number of processors
   currently available in the system.  This may be less than the
   number returned by get_nprocs_conf() because processors may be
   offline (e.g., on hotpluggable systems).

On my devbox, I’m unable to reproduce a difference between these two values, even when using LXC with a cpuset restriction:

(venv) (base) ubuntu@devbox:~/ray$ getconf _NPROCESSORS_ONLN
48
(venv) (base) ubuntu@devbox:~/ray$ getconf _NPROCESSORS_CONF
48


(venv) (base) ubuntu@devbox:~/ray$ sudo cat /var/lib/lxc/cpu-test/config
# Template used to create this container: /usr/share/lxc/templates/lxc-download
# Parameters passed to the template:
# For additional config options, please look at lxc.container.conf(5)

# Uncomment the following line to support nesting containers:
#lxc.include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)


# Distribution configuration
lxc.include = /usr/share/lxc/config/common.conf
lxc.arch = linux64

# Container specific configuration
lxc.rootfs.path = dir:/var/lib/lxc/cpu-test/rootfs
lxc.uts.name = cpu-test

# Network configuration
lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:f6:04:ea
lxc.cgroup.cpuset.cpus = 0-1
lxc.cgroup2.cpuset.cpus = 0-1
(venv) (base) ubuntu@devbox:~/ray$ 
(venv) (base) ubuntu@devbox:~/ray$ 
(venv) (base) ubuntu@devbox:~/ray$ 
(venv) (base) ubuntu@devbox:~/ray$ 
(venv) (base) ubuntu@devbox:~/ray$  sudo lxc-attach -n cpu-test
root@cpu-test:/# 
root@cpu-test:/# 
root@cpu-test:/# 
root@cpu-test:/# getconf _NPROCESSORS_ONLN
48
root@cpu-test:/# getconf _NPROCESSORS_CONF
48

@Yicheng-Lu-llll

Copy link
Copy Markdown
Member

@morotti Could you please update your PR description with a screenshot showing the diff? Otherwise, LGTM.

@morotti

morotti commented Dec 16, 2025

Copy link
Copy Markdown
Contributor Author

@Yicheng-Lu-llll screenshot attached

image

@Yicheng-Lu-llll

Copy link
Copy Markdown
Member

@morotti Thanks!

@ZacAttack

Copy link
Copy Markdown
Contributor

So this has been open a while. My honest opinion on this is that we should absolutely change the variable IFF grpc also takes the change. Looking at the top of trunk version here: https://github.com/grpc/grpc/blob/ce8dc971ecd9451f8a462bd3d91b5c4a652a1f5c/src/core/util/linux/cpu.cc#L4

It seems they're using _SC_NPROCESSORS_CONF. It makes me nervous enough that we're patching this library, I'd rather not introduce additional delta. Once GRPC has taken this change we can make the tweak as well so we're as aligned as possible. But I'd prefer not to do that before.

I know that's annoying, but you do already have an out here. Setting RAY_num_grpc_internal_threads will set the cap on the threads in the mean time.

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

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants