FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-117657: Make frame clearing thread-safe by Fidget-Spinner · Pull Request #120542 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
29 changes: 29 additions & 0 deletions Lib/test/test_free_threading/test_frame.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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):
Comment thread
Fidget-Spinner marked this conversation as resolved.

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL