Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/mp/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,23 @@ SocketId StartSpawned(const SpawnConnectInfo& connect_info)

std::array<SocketId, 2> SocketPair()
{
// Set FD_CLOEXEC atomically when possible. Setting it with fcntl() after
// socketpair() leaves a window where a fork() in another thread can leak
// the descriptors into an unrelated child process. SpawnProcess relies on
// FD_CLOEXEC to close its error-reporting socket on successful exec, and
// SOCK_CLOEXEC sets the same descriptor flag.
int pair[2];
#ifdef SOCK_CLOEXEC
KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, pair));
#else
KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM, 0, pair));
KJ_SYSCALL(fcntl(pair[0], F_SETFD, FD_CLOEXEC));
KJ_SYSCALL(fcntl(pair[1], F_SETFD, FD_CLOEXEC));
if (fcntl(pair[0], F_SETFD, FD_CLOEXEC) == -1 || fcntl(pair[1], F_SETFD, FD_CLOEXEC) == -1) {
const int err = errno;
(void)close(pair[0]);
(void)close(pair[1]);
KJ_FAIL_SYSCALL("fcntl", err);
}
Comment on lines +364 to +369

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.

In commit "util: set FD_CLOEXEC atomically in SocketPair when SOCK_CLOEXEC is available" (e8c449e)

Note: not asking for a change here, this looks fine. But it is a good example of a tradeoff this file keeps running into: either use KJ_SYSCALL and leak descriptors when it throws, or add manual close() calls and hand-written error reporting like this. The same choice comes up in other places in this file, and it gets a lot worse with the Windows code in #231, which has many more error paths.

I think a better direction for followups would be to own descriptors with RAII from the moment they are created, so KJ_SYSCALL can throw freely without leaking, and only release them when ownership is handed off. KJ already provides kj::AutoCloseFd and, on Windows, kj::AutoCloseHandle for this. Here it could look like:

int pair[2];
KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM, 0, pair));
kj::AutoCloseFd fd0{pair[0]}, fd1{pair[1]};
KJ_SYSCALL(fcntl(fd0, F_SETFD, FD_CLOEXEC));
KJ_SYSCALL(fcntl(fd1, F_SETFD, FD_CLOEXEC));
return {fd0.release(), fd1.release()};

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.

re: #394 (comment)

I'd be happy to work on a follow-up with kj::AutoCloseFd after this merges, if you think that would be useful.

Yes, a follow-up would be welcome, thanks! Besides SocketPair(), SpawnProcess() is a good place for it: if the second SocketPair() call or connect_info_to_args() throws, the first socket pair is never closed.

#endif
return {pair[0], pair[1]};
}

Expand Down
Loading