Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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)
# Only restore signal handler if we're in the main thread
# signal.signal() only works in the main thread of the main interpreter
try:
signal.signal(signal.SIGWINCH, self.old_sigwinch)
except ValueError:
# This can happen when called from a non-main thread
# (e.g., asyncio REPL). In this case, we skip signal restoration
# to avoid the "signal only works in main thread" error.
pass
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 ValueError as e:
if "signal only works in main thread" in str(e):
exception_caught.append(e)
thread = threading.Thread(target=thread_target)
thread.start()
thread.join()
self.assertEqual(len(exception_caught), 0,
"restore() should not raise ValueError 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