8000 bpo-44645: Check for interrupts on any potentially backwards edge. (G… · python/cpython@000e70a · GitHub
[go: up one dir, main page]

Skip to content

Commit 000e70a

Browse files
authored
bpo-44645: Check for interrupts on any potentially backwards edge. (GH-27167)
1 parent d9f9232 commit 000e70a

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+
iterations = 100_000_000
1612+
1613+
def worker():
1614+
nonlocal iterations
1615+
nonlocal started
1616+
started = True
1617+
while cont:
1618+
if iterations:
1619+
iterations -= 1
1620+
else:
1621+
return
1622+
pass
1623+
1624+
t = threading.Thread(target=worker)
1625+
t.start()
1626+
while not started:
1627+
pass
1628+
cont = False
1629+
t.join()
1630+
self.assertNotEqual(iterations, 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+< 6BF4 /span>
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)
0