Repository navigation
[core] fix open leak for plasma store memory (shm/fallback) by workers - #52622
Merged
Merged
Conversation
leaked when the raylet forks processes. It is reproducible with spill and restore workers. The O_CLOEXEC flag ensures that the fd is closed on exec. Leaked fds can lead to disk space used by fallback allocate not being reclaimed until the worker process exits. Signed-off-by: irabbani <[email protected]>
edoakes
approved these changes
Apr 26, 2025
edoakes
left a comment
Collaborator
There was a problem hiding this comment.
LGTM. Thanks for digging into this and the detailed writeup.
Contributor
|
pretty interesting @israbbani it was found before that there were extra open fd's for the sockets from raylet forks. But the the shm / fallback fd's weren't thought of at that time. |
Contributor
This TODO suggests a different approach to fix the issue that we should explore. |
Contributor
Author
|
@jjyao agreed. I'm creating followups to
|
iamjustinhsu
pushed a commit
that referenced
this pull request
May 3, 2025
#52622) ### Description Open FDs for plasma store memory (in both shm and fallback can be leaked when the raylet forks processes. It is reproducible with spill and restore workers. The O_CLOEXEC flag ensures that the fd is closed on exec. Leaked fds can lead to disk space used by fallback allocate not being reclaimed until the worker process exits. ### Reproducing the Issue The issue was reported by a customer. I'm not putting their reproduction script here. I was able to trim it down to the following: ```python import time import ray import numpy as np @ray.remote def producer(i): print(f"Calling producer with {i}") return np.ones(1024 * 1024 * 512, dtype=np.uint8) @ray.remote def sleepy_consumer(i, obj_ref): print(F"Calling sleepy_consumer with {i}") time.sleep(2) print(F"Returning out of sleepy_consumer with {i}") obj_refs = [] for i in range(1): for _ in range(15): obj_refs.append(sleepy_consumer.remote(_, producer.remote(_))) for obj_ref in obj_refs: ray.get(obj_ref) ``` Start ray with an object store using 1G for shm ```bash ray start --head --object-store-memory=$((1024*1024*1024)) ``` Before starting the repro script ``` lsof +c0 +L1 | awk '{print $1 "\t" $2}' | sort | uniq -c 1 COMMAND PID 1 python3.11 3365522 1 python3.11 3365524 1 raylet 3365466 # dashboard agent has FD for plasma open ps aux | grep 3365522 ubuntu 3365522 1.8 0.1 3926348 122480 pts/4 Sl 00:00 0:01 /home/ubuntu/.conda/envs/ray2/bin/python3.11 -u /home/ubuntu/code/ray2/python/ray/dashboard/agent.py ... lsof -p 3365524 | grep -E "plasma" python3.1 3365524 ubuntu 17u REG 0,24 1073741832 2620 /dev/shm/plasmaQwZF2X (deleted) # runtime env agent has FD for plasma open ps aux | grep 3365524 ubuntu 3365524 0.4 0.1 2102056 80136 pts/4 Sl 00:00 0:00 /home/ubuntu/.conda/envs/ray2/bin/python3.11 -u /home/ubuntu/code/ray2/python/ray/_private/runtime_env/agent/main.py ... lsof -p 3365522 | grep -E "plasma" python3.1 3365522 ubuntu 17u REG 0,24 1073741832 2620 /dev/shm/plasmaQwZF2X (deleted) ``` The fds for shm/fallback are leaked to SPILL and RESTORE Workers ``` # Show the number of open files with zero links i.e. the OS is waiting for processes to close # them before reaping them and freeing up space. ubuntu@devbox:~/code/ray2$ lsof +c0 +L1 | awk '{print $1 "\t" $2}' | sort | uniq -c 1 COMMAND PID 1 python3.11 3365522 1 python3.11 3365524 1 ray::IDLE 3381018 15 ray::IDLE_Spill 3382481 15 ray::IDLE_Spill 3382485 15 ray::IDLE_Spill 3382486 15 ray::IDLE_Spill 3382487 1 raylet 3365466 ``` Space is still in use on /tmp for files that are allocated using fallback ``` (ray) ubuntu@devbox:~/code/ray$ df -h /tmp Filesystem Size Used Avail Use% Mounted on tmpfs 60G 12G 49G 20% /tmp ``` ### Investigating the Issue After tracing the code, there were no obvious candidates for leaks. Here are some basic facts that I discovered (captured for posterity): - Plasma Server creates temporary (unnamed files using the mkstemp syscall) for both shm and fallback allocations. - Plasma Server immediately unlinks the temporary file so it can be reaped by the OS when all open FDs are closed. - Spill and Restore workers are both CoreWorkers. This means they have a Plasma Client. - Plasma Server (in Raylet) sends FDs over the socket to Worker processes if they need to mmap the respective file (see examples for of [send](https://man7.org/tlpi/code/online/dist/sockets/scm_rights_send.c.html) and [recv](https://man7.org/tlpi/code/online/dist/sockets/scm_rights_recv.c.html)) - The Workers immediately close the FD after calling mmap on the file (this is the same for shm and fallback). I captured strace output for all possible syscalls that can leak to an FD being cloned: ```bash # get_trace.sh #!/bin/bash if [[ $# -lt 1 ]]; then echo "Usage: $0 <process_regex>" exit 1 fi while true; do pid=$(pgrep -i "$1" | head -n 1) if [[ -n "$pid" ]]; then echo "Attaching strace to PID $pid..." strace -s 2000 -yy -vvvtf -p "$pid" -e trace=open,openat,close,recvmsg -o "${1}_${pid}" break fi sleep 0.1 done ``` It looks like all FDs that were transferred over the socket as closed properly: ``` # FD received over the socket (ray) ubuntu@devbox:~/code/ray$ grep -iE "SCM_RIGH" Spill_2828327 | grep -o "tmp.*ed)" tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasma1JZuoX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasma2H1UKX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaqILGDW (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasma85QTLW (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaptEKFZ (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmahdVJGX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmarocAsX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasma7S5YMY (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaP3OI6Z (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaOHNNbZ (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmafwH9VZ (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaSAZlBX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmatVP3SY (deleted) # FD closed by the worker process (ray) ubuntu@devbox:~/code/ray$ grep -iE "close" Spill_2828327 | grep -o "tmp.*ed)" tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasma1JZuoX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasma2H1UKX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaqILGDW (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasma85QTLW (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaptEKFZ (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmahdVJGX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmarocAsX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasma7S5YMY (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaP3OI6Z (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaOHNNbZ (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmafwH9VZ (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmaSAZlBX (deleted) tmp/ray/session_2025-04-25_00-45-19_460552_2825286/plasmatVP3SY (deleted) ``` ### Fixing the Issue The only FDs that aren't explicitly opened or transfer are the ones inherited by the worker process when it is forked from the raylet. - the issue is non-deterministic because it depends on _when_ the Spill and Restore workers are forked - it's transient because the FDs are automatically closed when the worker exits The issue is fixed by using the O_CLOEXEC flag to the mkostemp syscall. This means the OS will close the FD when processes are forked from the raylet and exec-ed. Signed-off-by: irabbani <[email protected]> Signed-off-by: jhsu <[email protected]>
This was referenced Jan 8, 2026
This was referenced Jan 16, 2026
This was referenced Jan 27, 2026
Closed
This was referenced May 12, 2026
Closed
This was referenced May 28, 2026
This was referenced Aug 3, 2026
This was referenced Oct 1, 2026
This was referenced Oct 15, 2026
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.
Description
Open FDs for plasma store memory (in both shm and fallback can be leaked when the raylet forks processes. It is reproducible with spill and restore workers. The O_CLOEXEC flag ensures that the fd is closed on exec.
Leaked fds can lead to disk space used by fallback allocate not being reclaimed until the worker process exits.
Reproducing the Issue
The issue was reported by a customer. I'm not putting their reproduction script here. I was able to trim it down to the following:
Start ray with an object store using 1G for shm
ray start --head --object-store-memory=$((1024*1024*1024))Before starting the repro script
The fds for shm/fallback are leaked to SPILL and RESTORE Workers
Space is still in use on /tmp for files that are allocated using fallback
Investigating the Issue
After tracing the code, there were no obvious candidates for leaks. Here are some basic facts that I discovered (captured for posterity):
I captured strace output for all possible syscalls that can leak to an FD being cloned:
It looks like all FDs that were transferred over the socket as closed properly:
Fixing the Issue
The only FDs that aren't explicitly opened or transfer are the ones inherited by the worker process when it is forked from the raylet.
The issue is fixed by using the O_CLOEXEC flag to the mkostemp syscall. This means the OS will close the FD when processes are forked from the raylet and exec-ed.