Skip to content

Commit e16ec87

Browse files
author
Lutz Fischer
committed
Improve signal handler cleanup: call original handler if callable, else restore and re-raise signal
1 parent b7ffb38 commit e16ec87

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

dirlock/__init__.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ def handle_sigint_cleanup(signum, frame):
5252
_clean_locks()
5353
if original_sigint_handler is not None:
5454
# restore the original handler
55-
signal.signal(signal.SIGINT, original_sigint_handler)
56-
# and re-raise the signal
57-
os.kill(os.getpid(), signal.SIGINT)
55+
if callable(original_sigint_handler):
56+
original_sigint_handler(signum, frame)
57+
else:
58+
signal.signal(signal.SIGINT, original_sigint_handler)
59+
# and re-raise the signal
60+
os.kill(os.getpid(), signal.SIGINT)
5861

5962

6063
def handle_sigterm_cleanup(signum, frame):
@@ -66,10 +69,13 @@ def handle_sigterm_cleanup(signum, frame):
6669
global original_sigterm_handler
6770
_clean_locks()
6871
if original_sigterm_handler is not None:
69-
# restore the original handler
70-
signal.signal(signal.SIGTERM, original_sigterm_handler)
71-
# and re-raise the signal
72-
os.kill(os.getpid(), signal.SIGTERM)
72+
# restore the original handler
73+
if callable(original_sigterm_handler):
74+
original_sigterm_handler(signum, frame)
75+
else:
76+
signal.signal(signal.SIGTERM, original_sigterm_handler)
77+
# and re-raise the signal
78+
os.kill(os.getpid(), signal.SIGTERM)
7379

7480

7581
# normal exit clean up

0 commit comments

Comments
 (0)