Skip to content

Commit 37bdd22

Browse files
bpo-44645: Check for interrupts on any potentially backwards edge (GH-27216) (GH-27235)
(cherry picked from commit d09c134) Co-authored-by: Mark Shannon <mark@hotpy.org>
1 parent bce2847 commit 37bdd22

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Lib/test/test_threading.py

+25
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

+6-1
Original file line numberDiff line numberDiff line change
@@ -3759,14 +3759,17 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
37593759
if (Py_IsFalse(cond)) {
37603760
Py_DECREF(cond);
37613761
JUMPTO(oparg);
3762+
CHECK_EVAL_BREAKER();
37623763
DISPATCH();
37633764
}
37643765
err = PyObject_IsTrue(cond);
37653766
Py_DECREF(cond);
37663767
if (err > 0)
37673768
;
3768-
else if (err == 0)
3769+
else if (err == 0) {
37693770
JUMPTO(oparg);
3771+
CHECK_EVAL_BREAKER();
3772+
}
37703773
else
37713774
goto error;
37723775
DISPATCH();
@@ -3783,12 +3786,14 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
37833786
if (Py_IsTrue(cond)) {
37843787
Py_DECREF(cond);
37853788
JUMPTO(oparg);
3789+
CHECK_EVAL_BREAKER();
37863790
DISPATCH();
37873791
}
37883792
err = PyObject_IsTrue(cond);
37893793
Py_DECREF(cond);
37903794
if (err > 0) {
37913795
JUMPTO(oparg);
3796+
CHECK_EVAL_BREAKER();
37923797
}
37933798
else if (err == 0)
37943799
;

0 commit comments

Comments
 (0)