Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion Lib/_pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import signal
import struct
import termios
import threading
import time
import types
import platform
Expand Down Expand Up @@ -390,7 +391,15 @@ def restore(self):
os.write(self.output_fd, b"\033[?7h")

if hasattr(self, "old_sigwinch"):
signal.signal(signal.SIGWINCH, self.old_sigwinch)
if os.name != 'nt':
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.
if threading.current_thread() is threading.main_thread():
raise e
del self.old_sigwinch

def push_char(self, char: int | bytes) -> None:
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_pyrepl/test_unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,24 @@ def test_restore_with_invalid_environ_on_macos(self, _os_write):
console.prepare() # needed to call restore()
console.restore() # this should succeed

def test_restore_in_thread(self, _os_write):
# for gh-139391
import threading
console = unix_console([])
console.old_sigwinch = signal.SIG_DFL
exception_caught = []
def thread_target():
try:
console.restore()
except Exception as e:
exception_caught.append(e)
thread = threading.Thread(target=thread_target)
thread.start()
thread.join()
# gh-139391: should not raise any exception when called from non-main thread
self.assertEqual(len(exception_caught), 0,
"restore() should not raise any exception in non-main thread")


@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,2 @@
Fix crash in PyREPL asyncio mode when using Ctrl+Z (suspend) followed by
``fg`` (resume).
Loading