#include <torch/csrc/DataLoader.h>
#ifndef _WIN32
#include <torch/csrc/Exceptions.h>
#include <torch/csrc/utils/python_numbers.h>
#include <c10/util/irange.h>
#include <fmt/format.h>
#include <sys/wait.h>
#include <csignal>
#include <map>
#include <set>
#include <sstream>
using namespace torch;
#define SIGNAL_HANDLER(SIGNAL, HANDLER_NAME, ERROR_MSG) \
static void HANDLER_NAME(int sig, siginfo_t* info, void* ctx) { \
auto _w = \
write(STDERR_FILENO, ERROR_MSG, sizeof(ERROR_MSG) / sizeof(char)); \
(void)_w; \
struct sigaction sa{}; \
sa.sa_handler = SIG_DFL; \
sa.sa_flags = 0; \
if (sigemptyset(&sa.sa_mask) != 0 || \
sigaction(SIGNAL, &sa, nullptr) != 0) { \
_exit(EXIT_FAILURE); \
} else { \
raise(SIGNAL); \
} \
}
static void setSignalHandler(
int signal,
void (*handler)(int, siginfo_t*, void*),
struct sigaction* old_sa_ptr) {
struct sigaction sa{};
sa.sa_sigaction = handler;
sa.sa_flags = SA_RESTART | SA_SIGINFO | SA_NOCLDSTOP | SA_NODEFER;
if (sigemptyset(&sa.sa_mask) != 0 ||
sigaction(signal, &sa, old_sa_ptr) != 0) {
std::ostringstream oss;
oss << "An error occurred while setting handler for " << strsignal(signal)
<< '.';
TORCH_CHECK(false, std::move(oss).str());
}
}
SIGNAL_HANDLER(
SIGBUS,
handler_SIGBUS,
"ERROR: Unexpected bus error encountered in worker. "
"This might be caused by insufficient shared memory (shm).\n")
SIGNAL_HANDLER(
SIGSEGV,
handler_SIGSEGV,
"ERROR: Unexpected segmentation fault encountered in worker.\n")
SIGNAL_HANDLER(
SIGFPE,
handler_SIGFPE,
"ERROR: Unexpected floating-point exception encountered in worker.\n")
static void handler_SIGTERM(int , siginfo_t* info, void* ) {
if (info->si_pid == getppid()) {
_exit(EXIT_SUCCESS);
}
struct sigaction sa{};
sa.sa_handler = SIG_DFL;
sa.sa_flags = 0;
if (sigemptyset(&sa.sa_mask) != 0 || sigaction(SIGTERM, &sa, nullptr) != 0) {
_exit(EXIT_FAILURE);
} else {
raise(SIGTERM);
}
}
__attribute__((weak)) void setDataLoaderSignalHandlers() {}
static PyObject* THPModule_setWorkerSignalHandlers(
PyObject* ,
PyObject* ) {
HANDLE_TH_ERRORS
setSignalHandler(SIGBUS, &handler_SIGBUS, nullptr);
setSignalHandler(SIGSEGV, &handler_SIGSEGV, nullptr);
setSignalHandler(SIGTERM, &handler_SIGTERM, nullptr);
setSignalHandler(SIGFPE, &handler_SIGFPE, nullptr);
setDataLoaderSignalHandlers();
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static std::map<int64_t, std::set<pid_t>> worker_pids = {};
static PyObject* THPModule_errorIfAnyWorkerFails(
PyObject* ,
PyObject* ) {
HANDLE_TH_ERRORS
for (auto& w : worker_pids) {
auto& pid_set = w.second;
for (auto worker_pid : pid_set) {
siginfo_t infop{};
infop.si_pid = 0;
auto error =
waitid(P_PID, worker_pid, &infop, WEXITED | WNOHANG | WNOWAIT);
if (error < 0 || infop.si_pid == 0)
continue;
if (infop.si_code == CLD_EXITED &&
infop.si_status != EXIT_SUCCESS) {
auto error_msg = fmt::format(
"DataLoader worker (pid {}) exited unexpectedly with exit code {}. "
"Details are lost due to multiprocessing. Rerunning with "
"num_workers=0 may give better error trace.",
worker_pid,
infop.si_status);
pid_set.clear();
TORCH_CHECK(false, error_msg);
} else if (
infop.si_code == CLD_KILLED ||
infop.si_code == CLD_DUMPED) {
auto error_msg = fmt::format(
"DataLoader worker (pid {}) is killed by signal: {}. ",
worker_pid,
strsignal(infop.si_status));
if (infop.si_status == SIGBUS) {
error_msg +=
"It is possible that dataloader's workers are out of shared memory. "
"Please try to raise your shared memory limit.";
}
pid_set.clear();
TORCH_CHECK(false, error_msg);
}
}
}
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject* THPModule_setWorkerPIDs(PyObject* , PyObject* args) {
HANDLE_TH_ERRORS
TORCH_CHECK_TYPE(
PyTuple_GET_SIZE(args) == 2,
"_set_worker_pids expects exactly 2 arguments.");
int64_t key = THPUtils_unpackLong(PyTuple_GET_ITEM(args, 0));
TORCH_CHECK_VALUE(
worker_pids.find(key) == worker_pids.end(),
"_set_worker_pids should be called only once for each _BaseDataLoaderIter.");
PyObject* child_pids = PyTuple_GET_ITEM(args, 1);
TORCH_CHECK_TYPE(
PyTuple_Check(child_pids),
"_set_worker_pids expects a tuple for child_pids, but got ",
Py_TYPE(child_pids)->tp_name,
".");
std::set<pid_t> pids_set = {};
auto size = PyTuple_GET_SIZE(child_pids);
for (const auto idx : c10::irange(size)) {
PyObject* obj = PyTuple_GET_ITEM(child_pids, idx);
pids_set.insert(static_cast<pid_t>(THPUtils_unpackLong(obj)));
}
worker_pids[key] = pids_set;
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject* THPModule_removeWorkerPIDs(
PyObject* ,
PyObject* loader_id) {
HANDLE_TH_ERRORS
int64_t key = THPUtils_unpackLong(loader_id);
auto it = worker_pids.find(key);
TORCH_CHECK_VALUE(
it != worker_pids.end(),
"Cannot find worker information for _BaseDataLoaderIter with id ",
key);
worker_pids.erase(it);
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
#undef SIGNAL_HANDLER
#else
static PyObject* THPModule_setWorkerSignalHandlers(
PyObject* module,
PyObject* _ignored) {
Py_RETURN_NONE;
}
static PyObject* THPModule_setWorkerPIDs(PyObject* module, PyObject* _ignored) {
Py_RETURN_NONE;
}
static PyObject* THPModule_removeWorkerPIDs(
PyObject* module,
PyObject* _ignored) {
Py_RETURN_NONE;
}
static PyObject* THPModule_errorIfAnyWorkerFails(
PyObject* module,
PyObject* _ignored) {
Py_RETURN_NONE;
}
#endif
PyMethodDef DataLoaderMethods[] = {
{"_set_worker_signal_handlers",
THPModule_setWorkerSignalHandlers,
METH_NOARGS,
nullptr},
{"_set_worker_pids", THPModule_setWorkerPIDs, METH_VARARGS, nullptr},
{"_remove_worker_pids", THPModule_removeWorkerPIDs, METH_O, nullptr},
{"_error_if_any_worker_fails",
THPModule_errorIfAnyWorkerFails,
METH_NOARGS,
nullptr},
{nullptr, nullptr, 0, nullptr}};