Skip to content
Open
Show file tree
Hide file tree
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
36 changes: 15 additions & 21 deletions src/mp/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <fcntl.h>
#include <kj/common.h>
#include <kj/debug.h>
#include <kj/io.h>
#include <kj/string-tree.h>
#include <optional>
#include <pthread.h>
Expand Down Expand Up @@ -252,9 +253,11 @@ std::string LogEscape(const kj::StringTree& string, size_t max_size)
std::tuple<ProcessId, SocketId> 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.
//
Expand All @@ -268,21 +271,17 @@ std::tuple<ProcessId, SocketId> 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");
}
Expand Down Expand Up @@ -321,22 +320,19 @@ std::tuple<ProcessId, SocketId> 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<SpawnError> 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)
Expand All @@ -359,16 +355,14 @@ std::array<SocketId, 2> 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<std::string>& args)
Expand Down
21 changes: 21 additions & 0 deletions test/mp/test/spawn_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include <condition_variable>
#include <csignal>
#include <cstdlib>
#include <fcntl.h>
#include <mutex>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
Expand Down Expand Up @@ -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<std::string> {
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
Loading