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