Skip to content

Commit d09c134

Browse files
authored
bpo-44645: Check for interrupts on any potentially backwards edge (GH-27216)
1 parent aab1899 commit d09c134

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Lib/test/test_threading.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,31 @@ def test_interrupt_main_invalid_signal(self):
16041604
self.assertRaises(ValueError, _thread.interrupt_main, signal.NSIG)
16051605
self.assertRaises(ValueError, _thread.interrupt_main, 1000000)
16061606

1607+
@threading_helper.reap_threads
1608+
def test_can_interrupt_tight_loops(self):
1609+
cont = [True]
1610+
started = [False]
1611+
interrupted = [False]
1612+
1613+
def worker(started, cont, interrupted):
1614+
iterations = 100_000_000
1615+
started[0] = True
1616+
while cont[0]:
1617+
if iterations:
1618+
iterations -= 1
1619+
else:
1620+
return
1621+
pass
1622+
interrupted[0] = True
1623+
1624+
t = threading.Thread(target=worker,args=(started, cont, interrupted))
1625+
t.start()
1626+
while not started[0]:
1627+
pass
1628+
cont[0] = False
1629+
t.join()
1630+
self.assertTrue(interrupted[0])
1631+
16071632

16081633
class AtexitTests(unittest.TestCase):
16091634

Python/ceval.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3638,14 +3638,17 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
36383638
if (Py_IsFalse(cond)) {
36393639
Py_DECREF(cond);
36403640
JUMPTO(oparg);
3641+
CHECK_EVAL_BREAKER();
36413642
DISPATCH();
36423643
}
36433644
err = PyObject_IsTrue(cond);
36443645
Py_DECREF(cond);
36453646
if (err > 0)
36463647
;
3647-
else if (err == 0)
3648+
else if (err == 0) {
36483649
JUMPTO(oparg);
3650+
CHECK_EVAL_BREAKER();
3651+
}
36493652
else
36503653
goto error;
36513654
DISPATCH();
@@ -3662,12 +3665,14 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
36623665
if (Py_IsTrue(cond)) {
36633666
Py_DECREF(cond);
36643667
JUMPTO(oparg);
3668+
CHECK_EVAL_BREAKER();
36653669
DISPATCH();
36663670
}
36673671
err = PyObject_IsTrue(cond);
36683672
Py_DECREF(cond);
36693673
if (err > 0) {
36703674
JUMPTO(oparg);
3675+
CHECK_EVAL_BREAKER();
36713676
}
36723677
else if (err == 0)
36733678
;

0 commit comments

Comments
 (0)