diff options
author | bst-marge-bot <marge-bot@buildstream.build> | 2020-04-09 18:41:51 +0000 |
---|---|---|
committer | bst-marge-bot <marge-bot@buildstream.build> | 2020-04-09 18:41:51 +0000 |
commit | ef26306313fc15dcb9f2ce318c91c7b593be11f4 (patch) | |
tree | 4abadbafe36e005192bcdc19680941ac84dc3446 /src | |
parent | 358a6dd0c355e5c0c27d099d70c92fc6e9aeef9c (diff) | |
parent | 18c9cdd7bb0d8ff74168a7ebf180606a98507ed0 (diff) | |
download | buildstream-ef26306313fc15dcb9f2ce318c91c7b593be11f4.tar.gz |
Merge branch 'juerg/job-sigterm' into 'master'
Fix SIGTERM handling in job processes
See merge request BuildStream/buildstream!1861
Diffstat (limited to 'src')
-rw-r--r-- | src/buildstream/_scheduler/jobs/job.py | 16 | ||||
-rw-r--r-- | src/buildstream/_signals.py | 6 | ||||
-rw-r--r-- | src/buildstream/sandbox/_sandboxbuildboxrun.py | 2 |
3 files changed, 13 insertions, 11 deletions
diff --git a/src/buildstream/_scheduler/jobs/job.py b/src/buildstream/_scheduler/jobs/job.py index b4060910b..823404b59 100644 --- a/src/buildstream/_scheduler/jobs/job.py +++ b/src/buildstream/_scheduler/jobs/job.py @@ -688,17 +688,15 @@ class ChildJob: nonlocal starttime starttime += datetime.datetime.now() - stopped_time + # Graciously handle sigterms. + def handle_sigterm(): + self._child_shutdown(_ReturnCode.TERMINATED) + # Time, log and and run the action function # - with _signals.suspendable(stop_time, resume_time), self._messenger.recorded_messages( - self._logfile, self._logdir - ) as filename: - - # Graciously handle sigterms. - def handle_sigterm(_signum, _sigframe): - self._child_shutdown(_ReturnCode.TERMINATED) - - signal.signal(signal.SIGTERM, handle_sigterm) + with _signals.terminator(handle_sigterm), _signals.suspendable( + stop_time, resume_time + ), self._messenger.recorded_messages(self._logfile, self._logdir) as filename: self.message(MessageType.START, self.action_name, logfile=filename) diff --git a/src/buildstream/_signals.py b/src/buildstream/_signals.py index 425a57239..969789e92 100644 --- a/src/buildstream/_signals.py +++ b/src/buildstream/_signals.py @@ -43,10 +43,14 @@ suspendable_stack = deque() # type: MutableSequence[Callable] # Per process SIGTERM handler def terminator_handler(signal_, frame): + exit_code = -1 + while terminator_stack: terminator_ = terminator_stack.pop() try: terminator_() + except SystemExit as e: + exit_code = e.code or 0 except: # noqa pylint: disable=bare-except # Ensure we print something if there's an exception raised when # processing the handlers. Note that the default exception @@ -62,7 +66,7 @@ def terminator_handler(signal_, frame): # Use special exit here, terminate immediately, recommended # for precisely this situation where child processes are teminated. - os._exit(-1) + os._exit(exit_code) # terminator() diff --git a/src/buildstream/sandbox/_sandboxbuildboxrun.py b/src/buildstream/sandbox/_sandboxbuildboxrun.py index d3b9e4da6..f6ecbeaa0 100644 --- a/src/buildstream/sandbox/_sandboxbuildboxrun.py +++ b/src/buildstream/sandbox/_sandboxbuildboxrun.py @@ -130,7 +130,7 @@ class SandboxBuildBoxRun(SandboxREAPI): proc.terminate() try: - proc.wait(20) + proc.wait(15) except psutil.TimeoutExpired: utils._kill_process_tree(process.pid) |