Skip to content

Commit 4fb9544

Browse files
blueyedlisroach
authored andcommitted
bpo-36250: ignore ValueError from signal in non-main thread (pythonGH-12251)
Authored-By: blueyed <github@thequod.de>
1 parent f7d2e2b commit 4fb9544

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

Lib/pdb.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,12 @@ def preloop(self):
340340
def interaction(self, frame, traceback):
341341
# Restore the previous signal handler at the Pdb prompt.
342342
if Pdb._previous_sigint_handler:
343-
signal.signal(signal.SIGINT, Pdb._previous_sigint_handler)
344-
Pdb._previous_sigint_handler = None
343+
try:
344+
signal.signal(signal.SIGINT, Pdb._previous_sigint_handler)
345+
except ValueError: # ValueError: signal only works in main thread
346+
pass
347+
else:
348+
Pdb._previous_sigint_handler = None
345349
if self.setup(frame, traceback):
346350
# no interaction desired at this time (happens if .pdbrc contains
347351
# a command like "continue")

Lib/test/test_pdb.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,35 @@ def start_pdb():
13331333
self.assertNotIn('Error', stdout.decode(),
13341334
"Got an error running test script under PDB")
13351335

1336+
def test_issue36250(self):
1337+
1338+
with open(support.TESTFN, 'wb') as f:
1339+
f.write(textwrap.dedent("""
1340+
import threading
1341+
import pdb
1342+
1343+
evt = threading.Event()
1344+
1345+
def start_pdb():
1346+
evt.wait()
1347+
pdb.Pdb(readrc=False).set_trace()
1348+
1349+
t = threading.Thread(target=start_pdb)
1350+
t.start()
1351+
pdb.Pdb(readrc=False).set_trace()
1352+
evt.set()
1353+
t.join()""").encode('ascii'))
1354+
cmd = [sys.executable, '-u', support.TESTFN]
1355+
proc = subprocess.Popen(cmd,
1356+
stdout=subprocess.PIPE,
1357+
stdin=subprocess.PIPE,
1358+
stderr=subprocess.STDOUT,
1359+
)
1360+
self.addCleanup(proc.stdout.close)
1361+
stdout, stderr = proc.communicate(b'cont\ncont\n')
1362+
self.assertNotIn('Error', stdout.decode(),
1363+
"Got an error running test script under PDB")
1364+
13361365
def test_issue16180(self):
13371366
# A syntax error in the debuggee.
13381367
script = "def f: pass\n"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Ignore ``ValueError`` from ``signal`` with ``interaction`` in non-main
2+
thread.

0 commit comments

Comments
 (0)