Skip to content

Commit 94ff4d4

Browse files
ambvyihong0618
andauthored
[3.13] gh-139391: properly handle signal.signal() in UnixConsole when called from a non-main thread (GH-139392) (#139861)
(cherry picked from commit b8c8b8f) Co-authored-by: yihong <[email protected]>
1 parent fa49c2a commit 94ff4d4

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Lib/_pyrepl/unix_console.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,12 @@ def restore(self):
397397
os.write(self.output_fd, b"\033[?7h")
398398

399399
if hasattr(self, "old_sigwinch"):
400-
signal.signal(signal.SIGWINCH, self.old_sigwinch)
400+
try:
401+
signal.signal(signal.SIGWINCH, self.old_sigwinch)
402+
except ValueError as e:
403+
import threading
404+
if threading.current_thread() is threading.main_thread():
405+
raise e
401406
del self.old_sigwinch
402407

403408
def push_char(self, char: int | bytes) -> None:

Lib/test/test_pyrepl/test_unix_console.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import signal
55
import subprocess
66
import sys
7+
import threading
78
import unittest
89
from functools import partial
910
from test import support
1011
from test.support import os_helper
11-
from test.support import script_helper
12+
from test.support import script_helper, threading_helper
1213

1314
from unittest import TestCase
1415
from unittest.mock import MagicMock, call, patch, ANY, Mock
@@ -343,6 +344,17 @@ def test_restore_with_invalid_environ_on_macos(self, _os_write):
343344
console.prepare() # needed to call restore()
344345
console.restore() # this should succeed
345346

347+
@threading_helper.reap_threads
348+
@threading_helper.requires_working_threading()
349+
def test_restore_in_thread(self, _os_write):
350+
# gh-139391: ensure that console.restore() silently suppresses
351+
# exceptions when calling signal.signal() from a non-main thread.
352+
console = unix_console([])
353+
console.old_sigwinch = signal.SIG_DFL
354+
thread = threading.Thread(target=console.restore)
355+
thread.start()
356+
thread.join() # this should not raise
357+
346358

347359
@unittest.skipIf(sys.platform == "win32", "No Unix console on Windows")
348360
class TestUnixConsoleEIOHandling(TestCase):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an issue when, on non-Windows platforms, it was not possible to
2+
gracefully exit a ``python -m asyncio`` process suspended by Ctrl+Z
3+
and later resumed by :manpage:`fg` other than with :manpage:`kill`.

0 commit comments

Comments
 (0)