Skip to content

Commit 0a634e3

Browse files
[3.13] gh-121546: Disable contextvar caching on free-threading build (GH-121740) (#121808)
gh-121546: Disable contextvar caching on free-threading build (GH-121740) (cherry picked from commit e904300) Co-authored-by: Ken Jin <[email protected]>
1 parent b506de4 commit 0a634e3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Include/internal/pycore_context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ struct _pycontextvarobject {
3535
PyObject_HEAD
3636
PyObject *var_name;
3737
PyObject *var_default;
38+
#ifndef Py_GIL_DISABLED
3839
PyObject *var_cached;
3940
uint64_t var_cached_tsid;
4041
uint64_t var_cached_tsver;
42+
#endif
4143
Py_hash_t var_hash;
4244
};
4345

Python/context.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,15 @@ PyContextVar_Get(PyObject *ovar, PyObject *def, PyObject **val)
203203
goto not_found;
204204
}
205205

206+
#ifndef Py_GIL_DISABLED
206207
if (var->var_cached != NULL &&
207208
var->var_cached_tsid == ts->id &&
208209
var->var_cached_tsver == ts->context_ver)
209210
{
210211
*val = var->var_cached;
211212
goto found;
212213
}
214+
#endif
213215

214216
assert(PyContext_CheckExact(ts->context));
215217
PyHamtObject *vars = ((PyContext *)ts->context)->ctx_vars;
@@ -221,9 +223,11 @@ PyContextVar_Get(PyObject *ovar, PyObject *def, PyObject **val)
221223
}
222224
if (res == 1) {
223225
assert(found != NULL);
226+
#ifndef Py_GIL_DISABLED
224227
var->var_cached = found; /* borrow */
225228
var->var_cached_tsid = ts->id;
226229
var->var_cached_tsver = ts->context_ver;
230+
#endif
227231

228232
*val = found;
229233
goto found;
@@ -723,8 +727,10 @@ PyTypeObject PyContext_Type = {
723727
static int
724728
contextvar_set(PyContextVar *var, PyObject *val)
725729
{
730+
#ifndef Py_GIL_DISABLED
726731
var->var_cached = NULL;
727732
PyThreadState *ts = _PyThreadState_GET();
733+
#endif
728734

729735
PyContext *ctx = context_get();
730736
if (ctx == NULL) {
@@ -739,16 +745,20 @@ contextvar_set(PyContextVar *var, PyObject *val)
739745

740746
Py_SETREF(ctx->ctx_vars, new_vars);
741747

748+
#ifndef Py_GIL_DISABLED
742749
var->var_cached = val; /* borrow */
743750
var->var_cached_tsid = ts->id;
744751
var->var_cached_tsver = ts->context_ver;
752+
#endif
745753
return 0;
746754
}
747755

748756
static int
749757
contextvar_del(PyContextVar *var)
750758
{
759+
#ifndef Py_GIL_DISABLED
751760
var->var_cached = NULL;
761+
#endif
752762

753763
PyContext *ctx = context_get();
754764
if (ctx == NULL) {
@@ -823,9 +833,11 @@ contextvar_new(PyObject *name, PyObject *def)
823833

824834
var->var_default = Py_XNewRef(def);
825835

836+
#ifndef Py_GIL_DISABLED
826837
var->var_cached = NULL;
827838
var->var_cached_tsid = 0;
828839
var->var_cached_tsver = 0;
840+
#endif
829841

830842
if (_PyObject_GC_MAY_BE_TRACKED(name) ||
831843
(def != NULL && _PyObject_GC_MAY_BE_TRACKED(def)))
@@ -863,9 +875,11 @@ contextvar_tp_clear(PyContextVar *self)
863875
{
864876
Py_CLEAR(self->var_name);
865877
Py_CLEAR(self->var_default);
878+
#ifndef Py_GIL_DISABLED
866879
self->var_cached = NULL;
867880
self->var_cached_tsid = 0;
868881
self->var_cached_tsver = 0;
882+
#endif
869883
return 0;
870884
}
871885

0 commit comments

Comments
 (0)