Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88ed804

Browse files
[3.11] gh-71052: Use raise_signal in ThreadSignals.test_signals (GH-116423) (#116617)
gh-71052: Use `raise_signal` in `ThreadSignals.test_signals` (GH-116423) Use `raise_signal` rather than `kill` in `ThreadSignals.test_signals` (cherry picked from commit 34920f3) Co-authored-by: Malcolm Smith <[email protected]>
1 parent 6ae6d46 commit 88ed804

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

Lib/test/test_threadsignals.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,28 @@ def handle_signals(sig,frame):
3232

3333
# a function that will be spawned as a separate thread.
3434
def send_signals():
35-
os.kill(process_pid, signal.SIGUSR1)
36-
os.kill(process_pid, signal.SIGUSR2)
35+
# We use `raise_signal` rather than `kill` because:
36+
# * It verifies that a signal delivered to a background thread still has
37+
# its Python-level handler called on the main thread.
38+
# * It ensures the signal is handled before the thread exits.
39+
signal.raise_signal(signal.SIGUSR1)
40+
signal.raise_signal(signal.SIGUSR2)
3741
signalled_all.release()
3842

3943

4044
@threading_helper.requires_working_threading()
41-
@unittest.skipUnless(hasattr(signal, "alarm"), "test requires signal.alarm")
4245
class ThreadSignals(unittest.TestCase):
4346

4447
def test_signals(self):
4548
with threading_helper.wait_threads_exit():
4649
# Test signal handling semantics of threads.
47-
# We spawn a thread, have the thread send two signals, and
50+
# We spawn a thread, have the thread send itself two signals, and
4851
# wait for it to finish. Check that we got both signals
4952
# and that they were run by the main thread.
5053
signalled_all.acquire()
5154
self.spawnSignallingThread()
5255
signalled_all.acquire()
5356

54-
# the signals that we asked the kernel to send
55-
# will come back, but we don't know when.
56-
# (it might even be after the thread exits
57-
# and might be out of order.) If we haven't seen
58-
# the signals yet, send yet another signal and
59-
# wait for it return.
60-
if signal_blackboard[signal.SIGUSR1]['tripped'] == 0 \
61-
or signal_blackboard[signal.SIGUSR2]['tripped'] == 0:
62-
try:
63-
signal.alarm(1)
64-
signal.pause()
65-
finally:
66-
signal.alarm(0)
67-
6857
self.assertEqual( signal_blackboard[signal.SIGUSR1]['tripped'], 1)
6958
self.assertEqual( signal_blackboard[signal.SIGUSR1]['tripped_by'],
7059
thread.get_ident())

0 commit comments

Comments
 (0)