From 2f6e4323d332f7fabac53e72b9f57135e2916e9f Mon Sep 17 00:00:00 2001 From: Javier Mateos Date: Sat, 14 Nov 2026 14:06:56 +0000 Subject: [PATCH] util: manage SpawnProcess socket descriptors with RAII SpawnProcess could leak file descriptors if the second SocketPair(), connect_info_to_args, or MakeArgv threw an exception. Use kj::AutoCloseFd to take ownership of the descriptors as soon as they're created. Release them when closing or returning them, keeping the existing error handling and child behavior unchanged. Do the same in the SocketPair() fallback path used when SOCK_CLOEXEC is not available. Add a test that throws from the callback and checks that its socket is closed. --- src/mp/util.cpp | 36 +++++++++++++++--------------------- test/mp/test/spawn_tests.cpp | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/mp/util.cpp b/src/mp/util.cpp index e9710eaa..9ab3e496 100644 --- a/src/mp/util.cpp +++ b/src/mp/util.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -252,9 +253,11 @@ std::string LogEscape(const kj::StringTree& string, size_t max_size) std::tuple SpawnProcess(SpawnConnectInfoToArgsFn&& connect_info_to_args) { auto fds{SocketPair()}; + kj::AutoCloseFd fds0_owner{fds[0]}, fds1_owner{fds[1]}; // Only used for the child to report errors back to the parent, so a one-way // pipe would be sufficient, but reuse the existing SocketPair() helper. auto error_fds{SocketPair()}; + kj::AutoCloseFd error_fds0_owner{error_fds[0]}, error_fds1_owner{error_fds[1]}; // Evaluate the callback and build the argv array before forking. // @@ -268,21 +271,17 @@ std::tuple SpawnProcess(SpawnConnectInfoToArgsFn&& connect_ ProcessId pid = fork(); if (pid == -1) { const int err = errno; - (void)close(fds[0]); - (void)close(fds[1]); - (void)close(error_fds[0]); - (void)close(error_fds[1]); throw std::system_error(err, std::system_category(), "fork"); } + // The child keeps using the raw descriptors and never runs the + // AutoCloseFd destructors, because it only leaves via execvp() or _exit(). + // // Parent process closes the descriptor for socket 0, child closes the // descriptor for socket 1. On failure, the parent throws, but the child // must _exit(126) (post-fork child must not throw). - if (close(fds[pid ? 0 : 1]) != 0) { + if (close(pid ? fds0_owner.release() : fds[1]) != 0) { const int err = errno; if (pid) { - (void)close(fds[1]); - (void)close(error_fds[0]); - (void)close(error_fds[1]); KillAndReapChild(pid); throw std::system_error(err, std::system_category(), "close"); } @@ -321,22 +320,19 @@ std::tuple SpawnProcess(SpawnConnectInfoToArgsFn&& connect_ } // Close the parent's copy of the child's write end. - if (close(error_fds[0]) != 0) { + if (close(error_fds0_owner.release()) != 0) { const int err = errno; - (void)close(error_fds[1]); - (void)close(fds[1]); KillAndReapChild(pid); throw std::system_error(err, std::system_category(), "close"); } const std::optional error{ReadSpawnResult(error_fds[1])}; - (void)close(error_fds[1]); + (void)close(error_fds1_owner.release()); if (error) { - (void)close(fds[1]); KillAndReapChild(pid); throw std::system_error(error->err, std::system_category(), SpawnErrorName(error->which)); } - return {pid, fds[1]}; + return {pid, fds1_owner.release()}; } SocketId StartSpawned(const SpawnConnectInfo& connect_info) @@ -359,16 +355,14 @@ std::array SocketPair() int pair[2]; #ifdef SOCK_CLOEXEC KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, pair)); + return {pair[0], pair[1]}; #else KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM, 0, pair)); - 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); - } + kj::AutoCloseFd fd0{pair[0]}, fd1{pair[1]}; + KJ_SYSCALL(fcntl(fd0.get(), F_SETFD, FD_CLOEXEC)); + KJ_SYSCALL(fcntl(fd1.get(), F_SETFD, FD_CLOEXEC)); + return {fd0.release(), fd1.release()}; #endif - return {pair[0], pair[1]}; } ProcessId StartProcess(const std::vector& args) diff --git a/test/mp/test/spawn_tests.cpp b/test/mp/test/spawn_tests.cpp index 74cbaf06..11344a84 100644 --- a/test/mp/test/spawn_tests.cpp +++ b/test/mp/test/spawn_tests.cpp @@ -14,7 +14,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -131,5 +133,24 @@ KJ_TEST("SpawnProcess throws on execvp failure") KJ_EXPECT(std::string_view{e.what()}.find("execvp") != std::string_view::npos); } } + +KJ_TEST("SpawnProcess closes the socket passed to the callback when it throws") +{ + int callback_fd{-1}; + try { + SpawnProcess([&](SpawnConnectInfo connect_info) -> std::vector { + callback_fd = std::stoi(connect_info); + throw std::runtime_error("callback failed"); + }); + KJ_EXPECT(false, "expected SpawnProcess to throw"); + } catch (const std::runtime_error& e) { + KJ_EXPECT(std::string_view{e.what()} == "callback failed", e.what()); + } + KJ_EXPECT(callback_fd >= 0); + // The descriptor passed to the callback must have been closed. + const int ret{::fcntl(callback_fd, F_GETFD)}; + const int err{errno}; + KJ_EXPECT(ret == -1 && err == EBADF, ret, err); +} } // namespace test } // namespace mp