File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -866,11 +866,8 @@ contextvar_set(PyContextVar *var, PyObject *val)
866866 return -1 ;
867867 }
868868
869- // gh-148891: _PyHamt_Assoc can allocate HAMT nodes, which may trigger
870- // GC. A weakref callback or finalizer could then re-enter
871- // contextvar_set, replacing ctx->ctx_vars via Py_SETREF and freeing
872- // the old HAMT while _PyHamt_Assoc is still traversing it.
873- // Prevent this by holding a strong reference to the current vars.
869+ // gh-148891: _PyHamt_Assoc may allocate nodes, which may trigger GC.
870+ // Bump the reference count to prevent the HAMT from being freed during traversal.
874871 PyHamtObject * old_vars = context_get_current_vars (ctx );
875872 Py_INCREF (old_vars );
876873 PyHamtObject * new_vars = _PyHamt_Assoc (
@@ -902,8 +899,8 @@ contextvar_del(PyContextVar *var)
902899 return -1 ;
903900 }
904901
905- // gh-148891: same borrowed-reference hazard as contextvar_set:
906- // _PyHamt_Without allocates nodes, which may trigger GC re-entrancy .
902+ // gh-148891: _PyHamt_Without may allocate nodes, which may trigger GC.
903+ // Bump the reference count to prevent the HAMT from being freed during traversal .
907904 PyHamtObject * vars = context_get_current_vars (ctx );
908905 Py_INCREF (vars );
909906 PyHamtObject * new_vars = _PyHamt_Without (vars , (PyObject * )var );
You can’t perform that action at this time.
0 commit comments