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-121546: Disable contextvar caching on free-threading build #121740

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,15 @@
goto not_found;
}

#ifndef Py_GIL_DISABLED
if (var->var_cached != NULL &&
var->var_cached_tsid == ts->id &&
var->var_cached_tsver == ts->context_ver)
{
*val = var->var_cached;
goto found;
}
#endif

assert(PyContext_CheckExact(ts->context));
PyHamtObject *vars = ((PyContext *)ts->context)->ctx_vars;
Expand All @@ -221,9 +223,11 @@
}
if (res == 1) {
assert(found != NULL);
#ifndef Py_GIL_DISABLED
var->var_cached = found; /* borrow */
var->var_cached_tsid = ts->id;
var->var_cached_tsver = ts->context_ver;
#endif

*val = found;
goto found;
Expand Down Expand Up @@ -723,8 +727,10 @@
static int
contextvar_set(PyContextVar *var, PyObject *val)
{
#ifndef Py_GIL_DISABLED
var->var_cached = NULL;
#endif
PyThreadState *ts = _PyThreadState_GET();

Check warning on line 733 in Python/context.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test

unused variable ‘ts’ [-Wunused-variable]
Fidget-Spinner marked this conversation as resolved.
Show resolved Hide resolved

PyContext *ctx = context_get();
if (ctx == NULL) {
Expand All @@ -739,16 +745,20 @@

Py_SETREF(ctx->ctx_vars, new_vars);

#ifndef Py_GIL_DISABLED
var->var_cached = val; /* borrow */
var->var_cached_tsid = ts->id;
var->var_cached_tsver = ts->context_ver;
#endif
return 0;
}

static int
contextvar_del(PyContextVar *var)
{
#ifndef Py_GIL_DISABLED
var->var_cached = NULL;
#endif

PyContext *ctx = context_get();
if (ctx == NULL) {
Expand Down Expand Up @@ -823,9 +833,11 @@

var->var_default = Py_XNewRef(def);

#ifndef Py_GIL_DISABLED
var->var_cached = NULL;
var->var_cached_tsid = 0;
var->var_cached_tsver = 0;
#endif

if (_PyObject_GC_MAY_BE_TRACKED(name) ||
(def != NULL && _PyObject_GC_MAY_BE_TRACKED(def)))
Expand Down Expand Up @@ -863,9 +875,11 @@
{
Py_CLEAR(self->var_name);
Py_CLEAR(self->var_default);
#ifndef Py_GIL_DISABLED
self->var_cached = NULL;
self->var_cached_tsid = 0;
self->var_cached_tsver = 0;
#endif
return 0;
}

Expand Down
Loading