diff options
author | Mike Frysinger <vapier@google.com> | 2023-06-09 22:13:36 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2023-06-10 22:02:29 -0400 |
commit | c4291c3fb793a21c1f0046e40a97960ba4bd84dd (patch) | |
tree | 9138edbe6bf853c1961f0b8e0eeba459ce7a1e87 | |
parent | e669af6414ef37f30e6a27a50f80b77620c244df (diff) | |
download | repohooks-c4291c3fb793a21c1f0046e40a97960ba4bd84dd.tar.gz |
utils: handle signal errors with threads
We might not be able to rebind signals if we're running in a thread,
so ignore ValueError when setting the handler.
Bug: None
Test: unittests
Change-Id: I81a222bf00ff433c8570bb7058871817f15da003
-rw-r--r-- | rh/utils.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/rh/utils.py b/rh/utils.py index 157e31b..4f1a063 100644 --- a/rh/utils.py +++ b/rh/utils.py @@ -369,18 +369,27 @@ def run(cmd, redirect_stdout=False, redirect_stderr=False, cwd=None, input=None, old_sigint = signal.getsignal(signal.SIGINT) handler = functools.partial(_kill_child_process, proc, int_timeout, kill_timeout, cmd, old_sigint) - signal.signal(signal.SIGINT, handler) + # We have to ignore ValueError in case we're run from a thread. + try: + signal.signal(signal.SIGINT, handler) + except ValueError: + old_sigint = None old_sigterm = signal.getsignal(signal.SIGTERM) handler = functools.partial(_kill_child_process, proc, int_timeout, kill_timeout, cmd, old_sigterm) - signal.signal(signal.SIGTERM, handler) + try: + signal.signal(signal.SIGTERM, handler) + except ValueError: + old_sigterm = None try: (result.stdout, result.stderr) = proc.communicate(input) finally: - signal.signal(signal.SIGINT, old_sigint) - signal.signal(signal.SIGTERM, old_sigterm) + if old_sigint is not None: + signal.signal(signal.SIGINT, old_sigint) + if old_sigterm is not None: + signal.signal(signal.SIGTERM, old_sigterm) if popen_stdout: # The linter is confused by how stdout is a file & an int. |