Skip to content

Commit 8bb6eab

Browse files
committed
Merge #108: doc: Add comments for socket descriptor handling when forking
b56bf21 doc: Add comments for socket descriptor handling when forking (TheCharlatan) Pull request description: I find logic around forking child processes always a bit confusing, hopefully these comments can make it a bit easier for a future reader. ACKs for top commit: ryanofsky: Code review ACK b56bf21 Tree-SHA512: ce7d783d3cfd4b925c12459568e621f74e86ba7f38e534dd3d4e3fb728d743a13c32e3467f9402b16afa16045ff3270ae986019ca9b901c4460e46a61a9cc706
2 parents 17a2399 + b56bf21 commit 8bb6eab

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

src/mp/util.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args)
110110
if (pid == -1) {
111111
throw std::system_error(errno, std::system_category(), "fork");
112112
}
113+
// Parent process closes the descriptor for socket 0, child closes the descriptor for socket 1.
113114
if (close(fds[pid ? 0 : 1]) != 0) {
114115
throw std::system_error(errno, std::system_category(), "close");
115116
}
116117
if (!pid) {
118+
// Child process must close all potentially open descriptors, except socket 0.
117119
int maxFd = MaxFd();
118120
for (int fd = 3; fd < maxFd; ++fd) {
119121
if (fd != fds[0]) {

0 commit comments

Comments
 (0)