Skip to content

[core] Closes Unix Socket FDs on fork. - #41121

Merged
rkooo567 merged 11 commits into
ray-project:masterfrom
rynewang:close-fd-on-fork
Nov 20, 2023
Merged

rkooo567 merged 11 commits into
ray-project:masterfrom
rynewang:close-fd-on-fork

Conversation

@rynewang

@rynewang rynewang commented Nov 14, 2023 •

Copy link
Copy Markdown
Contributor

In this PR, we address the issue of unexpectedly high socket usage in ray core worker processes. The Raylet process listens on two Unix socket file descriptors (FDs): plasma_store and raylet. Each core worker connects to both sockets for operations such as shared memory mapping. Theoretically, for N core workers, the raylet process should maintain O(N) established sockets along with two listening sockets, while each core worker should have only two established sockets.

Contrary to these expectations, empirical observations reveal a quadratic growth in the number of sockets, where each worker exhibits O(N) socket connections. Our investigation, spanning over a week and involving extensive analysis of system calls such as dup, socket, and open, identified the fork syscall as the primary contributor to this anomaly. The fork syscall, known for causing child processes to inherit all file descriptors of the parent process, played a pivotal role in this issue. Despite the use of Boost ASIO for socket management, it failed to mitigate this inherited behavior. Consequently, we had to manually extract the file descriptor and mark it as "close on fork" using the FD_CLOEXEC flag set by fcntl.

To validate our hypothesis, we conducted a preliminary experiment using ss -x -a -p, which confirmed that the sockets were initially shared across multiple processes. Post-application of our proposed patch, this sharing was effectively ceased. This study not only resolves the socket proliferation issue, as documented in issue #40813, but also contributes to a deeper understanding of file descriptor management in forked processes within distributed systems.

Notably, ASIO stipulates that the caller of fork() should also invoke notify_fork() both before and after the fork operation. However, in our context, the absence of this procedure did not manifest any apparent anomalies, hence it was not included in our initial corrective measures.

Fixes #40813.

@rkooo567

rkooo567 commented Nov 14, 2023 •

Copy link
Copy Markdown
Contributor

fork happens as a part of starting a new worker process? Did I understand the problem correctly here?

  • Whenever we create a new worker, we fork.
  • We duplicates fds to workers.
  • Repeat
    ?

Also can we add a test? I assume we should be able to write pretty determinisitc tests?

@rkooo567

Copy link
Copy Markdown
Contributor

Btw, also found this message lol

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

@rynewang

Copy link
Copy Markdown
Contributor Author

That's right. When we fork, all open FDs in the raylet gets inherited to the child process aka the worker. The worker don't need them nor does it know them, so they never close, leading to leaks. Fun point is, if raylet had already connected with worker 1, when it forks worker 2, the new worker 2 would keep the sockets to worker 1, leading to O(n^2) open sockets.

Will make a unit test.

rynewang and others added 6 commits November 14, 2023 13:58
Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: Ruiyang Wang <[email protected]>
Comment thread src/ray/common/client_connection.cc Resolved
Comment thread src/ray/common/client_connection.cc Outdated
}
#else
void setFdCloseOnFork(int fd) {
int flags = fcntl(fd, F_GETFD, 0);

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.

Can you log when flags == -1?

Also, what's the implication when this fails actually? Should we check fail? Or we just tolerate leaks?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

== -1 means the fd does not exist. Yes we want to check it, will add

Comment thread src/ray/common/client_connection.cc Outdated
void setFdCloseOnFork(int fd) {
int flags = fcntl(fd, F_GETFD, 0);
if (flags != -1) {
fcntl(fd, F_SETFD, flags | FD_CLOEXEC);

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.

why do you do flags | FD_CLOEXEC instead of just FD_CLOEXEC?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this is for furture compatible. An FD may have more flags to set and here we only want to set FD_CLOEXEC. So we read out the other flags and keep them intact. In practice today we only have this FD_CLOEXEC single flag for a FD, but it's common practice and will support the future Linux 7.0 changes.

template <typename NativeHandleType>
void setFdCloseOnFork(const NativeHandleType &handle) {
// In Windows we don't need to do anything, beacuse in CreateProcess we pass
// bInheritHandles = false which means we don't inherit handles or sockets.

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.

Add a relevant link?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

typedef boost::asio::generic::stream_protocol local_stream_protocol;
typedef boost::asio::basic_stream_socket<local_stream_protocol> local_stream_socket;

void SetCloseOnFork(local_stream_socket &socket);

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.

Can you add docstrings?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

return;
}
#else
void setFdCloseOnFork(int fd) {

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.

what's happening if we call it on the same fd twice? No-op?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

idempotent. still sets to close-on-fork.

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.

can you comment to the API that it is idempotent?

Comment thread src/ray/common/client_connection.cc Outdated
void setFdCloseOnFork(int fd) {
int flags = fcntl(fd, F_GETFD, 0);
if (flags != -1) {
fcntl(fd, F_SETFD, flags | FD_CLOEXEC);

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.

also can you comment here that it is not thread-safe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread src/ray/util/process.cc
std::error_code &ec,
bool decouple,
const ProcessEnvironment &env) {
/// TODO: use io_service with boost asio notify_fork.

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.

can we do this just in a follow up PR? I think it is nice to follow the best practice

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok



@ray.remote
def fibonacci_and_assert_sockets(i):

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.

hmm test seems a little hard to understand. Is it impossible to just keep creating/killing actors instead? (and check # of sockets in actors doesn't grow)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added

Signed-off-by: Ruiyang Wang <[email protected]>

@rkooo567 rkooo567 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. plz follow up with #41121 (comment)!

@rkooo567

Copy link
Copy Markdown
Contributor

premerge failures

Signed-off-by: Ruiyang Wang <[email protected]>
@rynewang

Copy link
Copy Markdown
Contributor Author

premerge fixed

@rkooo567

Copy link
Copy Markdown
Contributor

premerge failure

@rynewang

Copy link
Copy Markdown
Contributor Author

premerge fixed

@rkooo567
rkooo567 merged commit 10db68e into ray-project:master Nov 20, 2023
@rkooo567

Copy link
Copy Markdown
Contributor

awesome!

ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Nov 29, 2023
In this PR, we address the issue of unexpectedly high socket usage in ray core worker processes. The Raylet process listens on two Unix socket file descriptors (FDs): plasma_store and raylet. Each core worker connects to both sockets for operations such as shared memory mapping. Theoretically, for N core workers, the raylet process should maintain O(N) established sockets along with two listening sockets, while each core worker should have only two established sockets.

Contrary to these expectations, empirical observations reveal a quadratic growth in the number of sockets, where each worker exhibits O(N) socket connections. Our investigation, spanning over a week and involving extensive analysis of system calls such as dup, socket, and open, identified the fork syscall as the primary contributor to this anomaly. The fork syscall, known for causing child processes to inherit all file descriptors of the parent process, played a pivotal role in this issue. Despite the use of Boost ASIO for socket management, it failed to mitigate this inherited behavior. Consequently, we had to manually extract the file descriptor and mark it as "close on fork" using the FD_CLOEXEC flag set by fcntl.

To validate our hypothesis, we conducted a preliminary experiment using ss -x -a -p, which confirmed that the sockets were initially shared across multiple processes. Post-application of our proposed patch, this sharing was effectively ceased. This study not only resolves the socket proliferation issue, as documented in issue ray-project#40813, but also contributes to a deeper understanding of file descriptor management in forked processes within distributed systems.

Notably, ASIO stipulates that the caller of fork() should also invoke notify_fork() both before and after the fork operation. However, in our context, the absence of this procedure did not manifest any apparent anomalies, hence it was not included in our initial corrective measures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Core] Idle workers have large number of file descriptors open to sockets/raylet and sockets/plasma_store

2 participants