[3.14] Revert "GH-91636: Clear weakrefs created by finalizers. (GH-136401) (#141993)" (#142152)

This commit is contained in:
Hugo van Kemenade 2025-12-01 20:34:37 +02:00 committed by GitHub
parent 4ce27904b5
commit 7642070807
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 14 additions and 54 deletions

View file

@ -1493,9 +1493,9 @@ move_legacy_finalizer_reachable(struct collection_state *state)
}
// Clear all weakrefs to unreachable objects. Weakrefs with callbacks are
// optionally enqueued in `wrcb_to_call`, but not invoked yet.
// enqueued in `wrcb_to_call`, but not invoked yet.
static void
clear_weakrefs(struct collection_state *state, bool enqueue_callbacks)
clear_weakrefs(struct collection_state *state)
{
PyObject *op;
WORKSTACK_FOR_EACH(&state->unreachable, op) {
@ -1527,10 +1527,6 @@ clear_weakrefs(struct collection_state *state, bool enqueue_callbacks)
_PyWeakref_ClearRef(wr);
_PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
if (!enqueue_callbacks) {
continue;
}
// We do not invoke callbacks for weakrefs that are themselves
// unreachable. This is partly for historical reasons: weakrefs
// predate safe object finalization, and a weakref that is itself
@ -2216,7 +2212,7 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,
interp->gc.long_lived_total = state->long_lived_total;
// Clear weakrefs and enqueue callbacks (but do not call them).
clear_weakrefs(state, true);
clear_weakrefs(state);
_PyEval_StartTheWorld(interp);
// Deallocate any object from the refcount merge step
@ -2227,19 +2223,11 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,
call_weakref_callbacks(state);
finalize_garbage(state);
_PyEval_StopTheWorld(interp);
// Handle any objects that may have resurrected after the finalization.
_PyEval_StopTheWorld(interp);
err = handle_resurrected_objects(state);
// Clear free lists in all threads
_PyGC_ClearAllFreeLists(interp);
if (err == 0) {
// Clear weakrefs to objects in the unreachable set. No Python-level
// code must be allowed to access those unreachable objects. During
// delete_garbage(), finalizers outside the unreachable set might
// run and create new weakrefs. If those weakrefs were not cleared,
// they could reveal unreachable objects.
clear_weakrefs(state, false);
}
_PyEval_StartTheWorld(interp);
if (err < 0) {