Skip to content

[core] fix open leak for plasma store memory (shm/fallback) by workers - #52622

Merged
edoakes merged 1 commit into
masterfrom
irabbani/CORE-1285
Apr 26, 2025
Merged

edoakes merged 1 commit into
masterfrom
irabbani/CORE-1285

Conversation

@israbbani

@israbbani israbbani commented Apr 25, 2025 •

Copy link
Copy Markdown
Contributor

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:

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

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 and recv)
  • 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:

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

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]>
@israbbani israbbani added the go add ONLY when ready to merge, run all tests label Apr 25, 2025
@israbbani
israbbani requested a review from a team April 26, 2025 00:26
@israbbani
israbbani marked this pull request as ready for review April 26, 2025 00:28

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

LGTM. Thanks for digging into this and the detailed writeup.

@edoakes
edoakes merged commit e164144 into master Apr 26, 2025
@edoakes
edoakes deleted the irabbani/CORE-1285 branch April 26, 2025 22:44
@dayshah

dayshah commented Apr 27, 2025

Copy link
Copy Markdown
Contributor

#41121
#40813

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.

@jjyao

jjyao commented Apr 28, 2025

Copy link
Copy Markdown
Contributor
// TODO(mehrdadn): Use clone() on Linux or posix_spawnp() on Mac to avoid duplicating
    // file descriptors into the child process, as that can be problematic.

This TODO suggests a different approach to fix the issue that we should explore.

@israbbani

Copy link
Copy Markdown
Contributor Author

@jjyao agreed. I'm creating followups to

  1. Make fd passing from raylet -> forked process opt-in i.e. you need to deliberately share FDs instead of passing all of them
  2. Write integration tests to detect leaks.

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]>
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

Development

Successfully merging this pull request may close these issues.

5 participants