Caffe2 - Python API
A deep learning, cross platform ML framework
signal_handling.py
1 r""""Signal handling for multiprocessing data loading.
2 
3 NOTE [ Signal handling in multiprocessing data loading ]
4 
5 In cases like DataLoader, if a worker process dies due to bus error/segfault
6 or just hang, the main process will hang waiting for data. This is difficult
7 to avoid on PyTorch side as it can be caused by limited shm, or other
8 libraries users call in the workers. In this file and `DataLoader.cpp`, we make
9 our best effort to provide some error message to users when such unfortunate
10 events happen.
11 
12 When a _DataLoaderIter starts worker processes, their pids are registered in a
13 defined in `DataLoader.cpp`: id(_DataLoaderIter) => Collection[ Worker pids ]
14 via `_set_worker_pids`.
15 
16 When an error happens in a worker process, the main process received a SIGCHLD,
17 and Python will eventually call the handler registered below
18 (in `_set_SIGCHLD_handler`). In the handler, the `_error_if_any_worker_fails`
19 call checks all registered worker pids and raise proper error message to
20 prevent main process from hanging waiting for data from worker.
21 
22 Additionally, at the beginning of each worker's `_utils.worker._worker_loop`,
23 `_set_worker_signal_handlers` is called to register critical signal handlers
24 (e.g., for SIGSEGV, SIGBUS, SIGFPE, SIGTERM) in C, which just prints an error
25 message to stderr before triggering the default handler. So a message will also
26 be printed from the worker process when it is killed by such signals.
27 
28 See NOTE [ Data Loader Multiprocessing Shutdown Logic ] for the reasoning of
29 this signal handling design and other mechanism we implement to make our
30 multiprocessing data loading robust to errors.
31 """
32 
33 import signal
34 import threading
35 import torch
36 from torch._C import _set_worker_pids, _remove_worker_pids, \
37  _error_if_any_worker_fails, _set_worker_signal_handlers
38 from . import IS_WINDOWS
39 
40 
41 _SIGCHLD_handler_set = False
42 r"""Whether SIGCHLD handler is set for DataLoader worker failures. Only one
43 handler needs to be set for all DataLoaders in a process."""
44 
45 
46 def _set_SIGCHLD_handler():
47  # Windows doesn't support SIGCHLD handler
48  if IS_WINDOWS:
49  return
50  # can't set signal in child threads
51  if not isinstance(threading.current_thread(), threading._MainThread):
52  return
53  global _SIGCHLD_handler_set
54  if _SIGCHLD_handler_set:
55  return
56  previous_handler = signal.getsignal(signal.SIGCHLD)
57  if not callable(previous_handler):
58  # This doesn't catch default handler, but SIGCHLD default handler is a
59  # no-op.
60  previous_handler = None
61 
62  def handler(signum, frame):
63  # This following call uses `waitid` with WNOHANG from C side. Therefore,
64  # Python can still get and update the process status successfully.
65  _error_if_any_worker_fails()
66  if previous_handler is not None:
67  previous_handler(signum, frame)
68 
69  signal.signal(signal.SIGCHLD, handler)
70  _SIGCHLD_handler_set = True