Skip to content
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

gh-117657: Make frame clearing thread-safe #120542

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
31 changes: 31 additions & 0 deletions Lib/test/test_free_threading/test_frame.py
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):
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved

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()
15 changes: 13 additions & 2 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "pycore_moduleobject.h" // _PyModule_GetDict()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode_metadata.h" // _PyOpcode_Deopt, _PyOpcode_Caches

#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION

#include "frameobject.h" // PyFrameObject
#include "pycore_frame.h"
Expand Down Expand Up @@ -1662,7 +1662,7 @@ frame_tp_clear(PyFrameObject *f)
}

static PyObject *
frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
frame_clear_unlocked(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
{
if (f->f_frame->owner == FRAME_OWNED_BY_GENERATOR) {
PyGenObject *gen = _PyFrame_GetGenerator(f->f_frame);
Expand Down Expand Up @@ -1692,6 +1692,17 @@ frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
return NULL;
}

static PyObject *
frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
{
PyObject *res;
// Another materialized frame might be racing on clearing the frame.
Py_BEGIN_CRITICAL_SECTION(f);
res = frame_clear_unlocked(f, NULL);
Py_END_CRITICAL_SECTION();
return res;
}

PyDoc_STRVAR(clear__doc__,
"F.clear(): clear most references held by the frame");

Expand Down
12 changes: 11 additions & 1 deletion Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -119,7 +120,7 @@ _PyGen_Finalize(PyObject *self)
}

static void
gen_dealloc(PyGenObject *gen)
gen_dealloc_no_lock(PyGenObject *gen)
{
PyObject *self = (PyObject *) gen;

Expand Down Expand Up @@ -158,6 +159,15 @@ gen_dealloc(PyGenObject *gen)
PyObject_GC_Del(gen);
}

static void
gen_dealloc(PyGenObject *gen)
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem right to me. tp_dealloc can't be called concurrently on an object.

Copy link
Member Author

Choose a reason for hiding this comment

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

In this case the racing part is mainly _PyFrame_ClearExceptCode and setting the generator's flags. _PyEval_FrameClearAndPop can race with this as it also clears a generator, but instead it's done by ceval.c.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand -- how can _PyFrame_ClearExceptCode race with gen_dealloc?

It looks like gen_dealloc is only called from the tp_dealloc handler. If an object is being destroyed, then no other thread has access to it (or there's a separate serious bug).

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm I'm not too sure either. Let me revert this fix for now and try to hunt it down again.

{
// 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)
Expand Down
Loading