Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Lib/_pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,15 @@ def restore(self):
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:
# We can silence the ValueError if signal.signal() raised it
# from a non-main thread on a non-Windows platform. Otherwise,
# we need to re-raise it as its cause could be different.
import threading
if threading.current_thread() is threading.main_thread():
raise e
del self.old_sigwinch

def push_char(self, char: int | bytes) -> None:
Expand Down
14 changes: 13 additions & 1 deletion Lib/test/test_pyrepl/test_unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import signal
import subprocess
import sys
import threading
import unittest
from functools import partial
from test.support import os_helper, force_not_colorized_test_class
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
Expand Down Expand Up @@ -317,6 +318,17 @@ def test_restore_with_invalid_environ_on_macos(self, _os_write):
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Loading