8000 bpo-44645: Check for interrupts on any potentially backwards edge. by markshannon · Pull Request #27167 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-44645: Check for interrupts on any potentially backwards edge. #27167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,31 @@ def test_interrupt_main_invalid_signal(self):
self.assertRaises(ValueError, _thread.interrupt_main, signal.NSIG)
self.assertRaises(ValueError, _thread.interrupt_main, 1000000)

@threading_helper.reap_threads
def test_can_interrupt_tight_loops(self):
cont = True
started = False
iterations = 100_000_000

def worker():
nonlocal iterations
nonlocal started
started = True
while cont:
if iterations:
iterations -= 1
else:
return
pass

t = threading.Thread(target=worker)
t.start()
while not started:
pass
cont = False
t.join()
self.assertNotEqual(iterations, 0)
Comment on lines +1607 to +1630
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@threading_helper.reap_threads
def test_can_interrupt_tight_loops(self):
cont = True
started = False
iterations = 100_000_000
def worker():
nonlocal iterations
nonlocal started
started = True
while cont:
if iterations:
iterations -= 1
else:
return
pass
t = threading.Thread(target=worker)
t.start()
while not started:
pass
cont = False
t.join()
self.assertNotEqual(iterations, 0)
@threading_helper.reap_threads
def test_can_interrupt_tight_loops(self):
cont = True
started = threading.Event()
iterations = 100_000_000
def worker():
nonlocal iterations
started.set()
while cont:
if iterations:
iterations -= 1
else:
return
pass
t = threading.Thread(target=worker)
t.start()
started.wait()
cont = False
t.join()
self.assertNotEqual(iterations, 0)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are testing whether tight loops can be interrupted. Adding all the thread event machinery could undermine that.
I'd rather keep the test as close to the original bug report as possible. A little bit of active waiting shouldn't be a problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I don't want to hold this more but note that the event is outside the loop, so it should not interfere



class AtexitTests(unittest.TestCase):

Expand Down
7 changes: 6 additions & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3638,14 +3638,17 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
if (Py_IsFalse(cond)) {
Py_DECREF(cond);
JUMPTO(oparg);
CHECK_EVAL_BREAKER();
DISPATCH();
}
err = PyObject_IsTrue(cond);
Py_DECREF(cond);
if (err > 0)
;
else if (err == 0)
else if (err == 0) {
JUMPTO(oparg);
CHECK_EVAL_BREAKER();
}
else
goto error;
DISPATCH();
Expand All @@ -3662,12 +3665,14 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
if (Py_IsTrue(cond)) {
Py_DECREF(cond);
JUMPTO(oparg);
CHECK_EVAL_BREAKER();
DISPATCH();
}
err = PyObject_IsTrue(cond);
Py_DECREF(cond);
if (err > 0) {
JUMPTO(oparg);
CHECK_EVAL_BREAKER();
}
else if (err == 0)
;
Expand Down
0