Skip to content

Commit 25f4c7e

Browse files
committed
pythongh-126434: What should be done if sys.exit() is called from a signal handler (on a non-main thread)?
1 parent 574e91c commit 25f4c7e

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎Lib/signal.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def stop_signal_thread():
8181
global _signal_thread, _signal_queue
8282
if _signal_thread is not None:
8383
_signal_queue.put('STOP_SIGNAL_HANDLER')
84+
_signal_thread.join()
85+
_signal_thread = None
8486

8587
def _signal_queue_handler():
8688
try:
@@ -98,12 +100,15 @@ def _signal_queue_handler():
98100
_log_missing_signal_handler(signo)
99101
except Exception:
100102
traceback.print_exc()
101-
pass
103+
except SystemExit:
104+
pass # TODO: what should be done in the event of a handler calling `sys.exit()`?
102105
except:
103-
pass
106+
traceback.print_exc()
107+
# import _thread
108+
# _thread.interrupt_main()
109+
# print(dir(threading.main_thread()))
104110
finally:
105-
global _signal_thread
106-
_signal_thread = None
111+
pass
107112

108113
# Similar to functools.wraps(), but only assign __doc__.
109114
# __module__ should be preserved,
@@ -132,6 +137,8 @@ def signal(signalnum, handler, use_dedicated_thread=True):
132137
else:
133138
if signal_int in _signo_to_handler:
134139
del _signo_to_handler[signal_int]
140+
if 0 == len(_signo_to_handler):
141+
stop_signal_thread()
135142
handler = _signal.signal(signal_int, _enum_to_int(handler))
136143
return old_handler or _int_to_enum(handler, Handlers)
137144

‎Lib/test/support/threading_helper.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import threading
66
import time
77
import unittest
8+
import signal
89

910
from test import support
1011

@@ -28,6 +29,8 @@ def threading_setup():
2829
def threading_cleanup(*original_values):
2930
orig_count, orig_ndangling = original_values
3031

32+
signal.stop_signal_thread()
33+
3134
timeout = 1.0
3235
for _ in support.sleeping_retry(timeout, error=False):
3336
# Copy the thread list to get a consistent output. threading._dangling

‎Lib/test/test_asyncio/test_unix_events.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
def tearDownModule():
3636
asyncio.set_event_loop_policy(None)
37+
signal.stop_signal_thread()
3738

3839

3940
MOCK_ANY = mock.ANY

‎bug.py‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import threading
44
import time
55
import multiprocessing
6+
import sys
67

78
def sigint_self():
89
time.sleep(1)
@@ -18,24 +19,27 @@ def run_signal_handler_dedicated_thread():
1819
event = multiprocessing.Event()
1920
def sigint_handler(_signo, _stack_frame):
2021
try:
21-
x = 1 / 0
2222
print(f'{threading.current_thread().name}: sigint_handler is setting event')
23-
event.set()
23+
#event.set()
24+
sys.exit()
2425
finally:
2526
print(f'{threading.current_thread().name}: sigint_handler is done')
2627

2728
def sigterm_handler(_signo, _stack_frame):
2829
print(f'{threading.current_thread().name}: sigterm_handler is running')
2930
pass
3031

31-
signal.signal(signal.SIGTERM, sigterm_handler)
32-
signal.signal(signal.SIGINT, sigint_handler)
32+
signal.signal(signal.SIGINT, sigint_handler, True)
3333

3434
threading.Thread(target=sigint_self, daemon=True).start()
3535
threading.Thread(target=sigkill_self, daemon=True).start() # Used for debugging only.
3636

3737
print(f'{threading.current_thread().name}: Waiting on event. PID = {os.getpid()}')
38-
event.wait()
38+
while True:
39+
if event.is_set():
40+
break
41+
else:
42+
time.sleep(0.1)
3943
print(f'{threading.current_thread().name}: Waiting is done')
4044

4145
if __name__ == '__main__':

0 commit comments

Comments
 (0)