]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-139391: properly handle `signal.signal()` in `UnixConsole` when called...
authorŁukasz Langa <lukasz@langa.pl>
Thu, 9 Oct 2025 17:39:23 +0000 (18:39 +0100)
committerGitHub <noreply@github.com>
Thu, 9 Oct 2025 17:39:23 +0000 (23:09 +0530)
(cherry picked from commit b8c8b8f1d30767e5358ffa93d41e6e27ca60570d)

Co-authored-by: yihong <zouzou0208@gmail.com>
Lib/_pyrepl/unix_console.py
Lib/test/test_pyrepl/test_unix_console.py
Misc/NEWS.d/next/Library/2025-09-28-16-34-11.gh-issue-139391.nRFnmx.rst [new file with mode: 0644]

index f0cb950170a6badde8ce0f53b7b8026b164ad88b..89ba7b38994d64434973aaa83f7a340a927bcd07 100644 (file)
@@ -397,7 +397,12 @@ class UnixConsole(Console):
             os.write(self.output_fd, b"\033[?7h")
 
         if hasattr(self, "old_sigwinch"):
-            signal.signal(signal.SIGWINCH, self.old_sigwinch)
+            try:
+                signal.signal(signal.SIGWINCH, self.old_sigwinch)
+            except ValueError as e:
+                import threading
+                if threading.current_thread() is threading.main_thread():
+                    raise e
             del self.old_sigwinch
 
     def push_char(self, char: int | bytes) -> None:
index d40400f09f39b2b0588c2a0e8e212745cd9dec88..5738dddce858258cf68db190660ccdf6fd198ba4 100644 (file)
@@ -4,11 +4,12 @@ import os
 import signal
 import subprocess
 import sys
+import threading
 import unittest
 from functools import partial
 from test import support
 from test.support import os_helper
-from test.support import script_helper
+from test.support import script_helper, threading_helper
 
 from unittest import TestCase
 from unittest.mock import MagicMock, call, patch, ANY, Mock
@@ -343,6 +344,17 @@ class TestConsole(TestCase):
             console.prepare()  # needed to call restore()
             console.restore()  # this should succeed
 
+    @threading_helper.reap_threads
+    @threading_helper.requires_working_threading()
+    def test_restore_in_thread(self, _os_write):
+        # gh-139391: ensure that console.restore() silently suppresses
+        # exceptions when calling signal.signal() from a non-main thread.
+        console = unix_console([])
+        console.old_sigwinch = signal.SIG_DFL
+        thread = threading.Thread(target=console.restore)
+        thread.start()
+        thread.join()  # this should not raise
+
 
 @unittest.skipIf(sys.platform == "win32", "No Unix console on Windows")
 class TestUnixConsoleEIOHandling(TestCase):
diff --git a/Misc/NEWS.d/next/Library/2025-09-28-16-34-11.gh-issue-139391.nRFnmx.rst b/Misc/NEWS.d/next/Library/2025-09-28-16-34-11.gh-issue-139391.nRFnmx.rst
new file mode 100644 (file)
index 0000000..93d1ce6
--- /dev/null
@@ -0,0 +1,3 @@
+Fix an issue when, on non-Windows platforms, it was not possible to
+gracefully exit a ``python -m asyncio`` process suspended by Ctrl+Z
+and later resumed by :manpage:`fg` other than with :manpage:`kill`.