gh-139391: properly handle signal.signal() in UnixConsole when called from a non-main thread (#139392)

This commit is contained in:
yihong 2025-10-09 22:58:01 +08:00 committed by GitHub
parent 81959a0364
commit b8c8b8f1d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 2 deletions

View file

@ -4,11 +4,12 @@
import signal
import subprocess
import sys
import threading
import unittest
from functools import partial
from test import support
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
@ -318,6 +319,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):