Skip to content

Commit d6ac3bf

Browse files
chore: shorten comment
1 parent 68e43d0 commit d6ac3bf

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

‎Python/context.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)