diff options
author | Angelos Evripiotis <jevripiotis@bloomberg.net> | 2017-12-13 09:47:05 +0000 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-12-13 12:56:49 -0500 |
commit | 030b8fb0eff2ba101f67509004d41d717ab8c2a4 (patch) | |
tree | c42bbf41ce4c7e8d19ac45a8a023db2c2c6c9689 | |
parent | f15874d1fcaed578f5ec6d89b1f97c9b5e03e20e (diff) | |
download | buildstream-030b8fb0eff2ba101f67509004d41d717ab8c2a4.tar.gz |
_signals: always pop handlers, and restore states
-rw-r--r-- | buildstream/_signals.py | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/buildstream/_signals.py b/buildstream/_signals.py index b8fd74e07..65841df89 100644 --- a/buildstream/_signals.py +++ b/buildstream/_signals.py @@ -65,11 +65,12 @@ def terminator(terminate_func): if outermost: original_handler = signal.signal(signal.SIGTERM, terminator_handler) - yield - - if outermost: - signal.signal(signal.SIGTERM, original_handler) - terminator_stack.pop() + try: + yield + finally: + if outermost: + signal.signal(signal.SIGTERM, original_handler) + terminator_stack.pop() # Just a simple object for holding on to two callbacks @@ -125,12 +126,13 @@ def suspendable(suspend_callback, resume_callback): if outermost: original_stop = signal.signal(signal.SIGTSTP, suspend_handler) - yield - - if outermost: - signal.signal(signal.SIGTSTP, original_stop) + try: + yield + finally: + if outermost: + signal.signal(signal.SIGTSTP, original_stop) - suspendable_stack.pop() + suspendable_stack.pop() # blocked() @@ -154,11 +156,12 @@ def blocked(signal_list, ignore=True): # Set and save the sigprocmask blocked_signals = signal.pthread_sigmask(signal.SIG_BLOCK, signal_list) - yield - - # If we have discarded the signals completely, this line will - # cause the discard_handler() to trigger for each signal in the list - signal.pthread_sigmask(signal.SIG_SETMASK, blocked_signals) + try: + yield + finally: + # If we have discarded the signals completely, this line will cause + # the discard_handler() to trigger for each signal in the list + signal.pthread_sigmask(signal.SIG_SETMASK, blocked_signals) # ignored() @@ -175,7 +178,8 @@ def ignored(signal_list): for sig in signal_list: orig_handlers[sig] = signal.signal(sig, signal.SIG_IGN) - yield - - for sig in signal_list: - signal.signal(sig, orig_handlers[sig]) + try: + yield + finally: + for sig in signal_list: + signal.signal(sig, orig_handlers[sig]) |