Repository navigation
[core] Closes Unix Socket FDs on fork. - #41121
Conversation
Signed-off-by: Ruiyang Wang <[email protected]>
|
fork happens as a part of starting a new worker process? Did I understand the problem correctly here?
Also can we add a test? I assume we should be able to write pretty determinisitc tests? |
|
Btw, also found this message lol |
|
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. |
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]>
| } | ||
| #else | ||
| void setFdCloseOnFork(int fd) { | ||
| int flags = fcntl(fd, F_GETFD, 0); |
There was a problem hiding this comment.
Can you log when flags == -1?
Also, what's the implication when this fails actually? Should we check fail? Or we just tolerate leaks?
There was a problem hiding this comment.
== -1 means the fd does not exist. Yes we want to check it, will add
| void setFdCloseOnFork(int fd) { | ||
| int flags = fcntl(fd, F_GETFD, 0); | ||
| if (flags != -1) { | ||
| fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
There was a problem hiding this comment.
why do you do flags | FD_CLOEXEC instead of just FD_CLOEXEC?
There was a problem hiding this comment.
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. |
| 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); |
| return; | ||
| } | ||
| #else | ||
| void setFdCloseOnFork(int fd) { |
There was a problem hiding this comment.
what's happening if we call it on the same fd twice? No-op?
There was a problem hiding this comment.
idempotent. still sets to close-on-fork.
There was a problem hiding this comment.
can you comment to the API that it is idempotent?
| void setFdCloseOnFork(int fd) { | ||
| int flags = fcntl(fd, F_GETFD, 0); | ||
| if (flags != -1) { | ||
| fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
There was a problem hiding this comment.
also can you comment here that it is not thread-safe?
| std::error_code &ec, | ||
| bool decouple, | ||
| const ProcessEnvironment &env) { | ||
| /// TODO: use io_service with boost asio notify_fork. |
There was a problem hiding this comment.
can we do this just in a follow up PR? I think it is nice to follow the best practice
|
|
||
|
|
||
| @ray.remote | ||
| def fibonacci_and_assert_sockets(i): |
There was a problem hiding this comment.
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)?
Signed-off-by: Ruiyang Wang <[email protected]>
rkooo567
left a comment
There was a problem hiding this comment.
LGTM. plz follow up with #41121 (comment)!
|
premerge failures |
Signed-off-by: Ruiyang Wang <[email protected]>
|
premerge fixed |
|
premerge failure |
Signed-off-by: Ruiyang Wang <[email protected]>
Signed-off-by: Ruiyang Wang <[email protected]>
|
premerge fixed |
|
awesome! |
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.
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_storeandraylet. 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, andopen, identified theforksyscall as the primary contributor to this anomaly. Theforksyscall, 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 byfcntl.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.