Skip to content
Closed
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
29 changes: 29 additions & 0 deletions Lib/test/test_free_threading/test_frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 getframe():
return sys._getframe()

foo = getframe()
def work():
for _ in range(4000):
foo.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