Skip to content

Commit 4fe8de0

Browse files
committed
Revise InternalDocs/garbage_collector.md.
1 parent a7551cf commit 4fe8de0

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

InternalDocs/garbage_collector.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,16 @@ Once the GC knows the list of unreachable objects, a very delicate process start
329329
with the objective of completely destroying these objects. Roughly, the process
330330
follows these steps in order:
331331

332-
1. Handle and clear weak references (if any). Weak references to unreachable objects
333-
are set to `None`. If the weak reference has an associated callback, the callback
334-
is enqueued to be called once the clearing of weak references is finished. We only
335-
invoke callbacks for weak references that are themselves reachable. If both the weak
336-
reference and the pointed-to object are unreachable we do not execute the callback.
337-
This is partly for historical reasons: the callback could resurrect an unreachable
338-
object and support for weak references predates support for object resurrection.
339-
Ignoring the weak reference's callback is fine because both the object and the weakref
340-
are going away, so it's legitimate to say the weak reference is going away first.
332+
1. Handle weak references with callbacks (if any). If the weak reference has
333+
an associated callback, the callback is enqueued to be called after the weak
334+
reference is cleared. We only invoke callbacks for weak references that
335+
are themselves reachable. If both the weak reference and the pointed-to
336+
object are unreachable we do not execute the callback. This is partly for
337+
historical reasons: the callback could resurrect an unreachable object
338+
and support for weak references predates support for object resurrection.
339+
Ignoring the weak reference's callback is fine because both the object and
340+
the weakref are going away, so it's legitimate to say the weak reference is
341+
going away first.
341342
2. If an object has legacy finalizers (`tp_del` slot) move it to the
342343
`gc.garbage` list.
343344
3. Call the finalizers (`tp_finalize` slot) and mark the objects as already
@@ -346,7 +347,12 @@ follows these steps in order:
346347
4. Deal with resurrected objects. If some objects have been resurrected, the GC
347348
finds the new subset of objects that are still unreachable by running the cycle
348349
detection algorithm again and continues with them.
349-
5. Call the `tp_clear` slot of every object so all internal links are broken and
350+
5. Clear any weak references that still refer to unreachable objects. The
351+
`wr_object` attribute for these weakrefs are set to `None`. Note that some
352+
of these weak references maybe have been newly created during the running of
353+
finalizers in step 3. Also, clear any weak references that are part of the
354+
unreachable set.
355+
6. Call the `tp_clear` slot of every object so all internal links are broken and
350356
the reference counts fall to 0, triggering the destruction of all unreachable
351357
objects.
352358

0 commit comments

Comments
 (0)