-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-117657: Make frame clearing thread-safe #120542
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import sys | ||
| import unittest | ||
| import threading | ||
|
|
||
| from test.support import threading_helper | ||
|
|
||
| NTHREADS = 6 | ||
|
|
||
| @threading_helper.requires_working_threading() | ||
| class TestFrame(unittest.TestCase): | ||
| def test_frame_clear_simultaneous(self): | ||
|
|
||
| def gen(): | ||
| for _ in range(10000): | ||
| return sys._getframe() | ||
|
|
||
| foo = gen() | ||
| def work(): | ||
| for _ in range(4000): | ||
| frame1 = foo | ||
| frame1.clear() | ||
|
|
||
|
|
||
| threads = [] | ||
| for i in range(NTHREADS): | ||
| thread = threading.Thread(target=work) | ||
| thread.start() | ||
| threads.append(thread) | ||
|
|
||
| for thread in threads: | ||
| thread.join() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_* | ||
| #include "pycore_pyerrors.h" // _PyErr_ClearExcState() | ||
| #include "pycore_pystate.h" // _PyThreadState_GET() | ||
| #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION | ||
|
|
||
| #include "pystats.h" | ||
|
|
||
|
|
@@ -119,7 +120,7 @@ _PyGen_Finalize(PyObject *self) | |
| } | ||
|
|
||
| static void | ||
| gen_dealloc(PyGenObject *gen) | ||
| gen_dealloc_no_lock(PyGenObject *gen) | ||
| { | ||
| PyObject *self = (PyObject *) gen; | ||
|
|
||
|
|
@@ -158,6 +159,15 @@ gen_dealloc(PyGenObject *gen) | |
| PyObject_GC_Del(gen); | ||
| } | ||
|
|
||
| static void | ||
| gen_dealloc(PyGenObject *gen) | ||
|
||
| { | ||
| // Another generator's finalizer might race on clearing the frame. | ||
| Py_BEGIN_CRITICAL_SECTION(gen); | ||
| gen_dealloc_no_lock(gen); | ||
| Py_END_CRITICAL_SECTION(); | ||
| } | ||
|
|
||
| static PySendResult | ||
| gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, | ||
| int exc, int closing) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.