Skip to content

Commit

Permalink
Merge d1e2584 into dbf744f
Browse files Browse the repository at this point in the history
  • Loading branch information
gshuflin committed Oct 9, 2020
2 parents dbf744f + d1e2584 commit fe9603c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/python/pants/base/exception_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,23 @@ def _toggle_ignoring_sigint(self, toggle: bool) -> None:
with self._ignore_sigint_lock:
self._ignoring_sigint = toggle

def handle_sigint(self, signum: int, _frame):
def _send_signal_to_children(self, received_signal: int, signame: str) -> None:
"""Send a signal to any children of this process in order.
Pants may have spawned multiple subprocesses via Python or Rust. Upon receiving a signal,
this method is invoked to propagate the signal to all children, regardless of how they were
spawned.
"""

self_process = psutil.Process()
children = self_process.children()
logger.debug(f"Sending SIGINT to child processes: {children}")
logger.debug(f"Sending signal {signame} ({received_signal}) to child processes: {children}")
for child_process in children:
child_process.send_signal(signal.SIGINT)
child_process.send_signal(received_signal)

def handle_sigint(self, signum: int, _frame):
ExceptionSink._signal_sent = signum
self._send_signal_to_children(int(signal.SIGTERM), "SIGTERM")
raise KeyboardInterrupt("User interrupted execution with control-c!")

# TODO(#7406): figure out how to let sys.exit work in a signal handler instead of having to raise
Expand All @@ -93,10 +102,12 @@ def __init__(self, signum, signame):

def handle_sigquit(self, signum, _frame):
ExceptionSink._signal_sent = signum
self._send_signal_to_children(int(signal.SIGQUIT), "SIGQUIT")
raise self.SignalHandledNonLocalExit(signum, "SIGQUIT")

def handle_sigterm(self, signum, _frame):
ExceptionSink._signal_sent = signum
self._send_signal_to_children(int(signal.SIGTERM), "SIGTERM")
raise self.SignalHandledNonLocalExit(signum, "SIGTERM")


Expand Down

0 comments on commit fe9603c

Please sign in to comment.