Repository navigation
gh-135552: Make the GC clear weakrefs later #136189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
42abb05
Make the GC clear weakrefs later.
nascheme 17a4f9e
Remove inaccurate comment.
nascheme 12f0b5c
Run clear_weakrefs() with world stopped.
nascheme 2f3daba
Defer weakref clears only for refs to classes.
nascheme 123bc25
Ensure weakrefs with callbacks are cleared early.
nascheme 496c0b1
Add NEWS.
nascheme 8a553d1
Add comment about wrlist iteration.
nascheme 01c882f
Merge 'origin/main' into gh-135552-wr-clear-later
nascheme 0060602
Merge 'origin/main' into gh-135552-wr-clear-later
nascheme 900022b
Revise NEWS.
nascheme 84bd123
Defer clear for weakrefs without callbacks.
nascheme bb29ea1
Add unit test for GH-132413.
nascheme 073409b
Add additional tests for weakref clearing.
nascheme 135223e
Revert unneeded changes to unit tests.
nascheme fe9e58a
Use assertIsNone() and assertIsNotNone().
nascheme 70ef387
Merge 'origin/main' into gh-135552-wr-clear-later
nascheme a7551cf
Merge 'origin/main' into gh-135552-wr-clear-later
nascheme 4fe8de0
Revise InternalDocs/garbage_collector.md.
nascheme 24fd972
Revise comments about weakref handling.
nascheme eca79ce
Merge branch 'origin/main' into gh-135552-wr-clear-later
nascheme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Revise comments about weakref handling.
Consolidate most of the comments related to handling of weakrefs. This avoids having the information repeated in different comments.
- Loading branch information
commit 24fd9729344c10d77dfab23b4a1e67c4aa7b42a9
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -862,6 +862,40 @@ move_legacy_finalizer_reachable(PyGC_Head *finalizers) | |
| * outside the unreachable set -- indeed, those are precisely the weakrefs | ||
| * whose callbacks must be invoked. See gc_weakref.txt for overview & some | ||
| * details. | ||
| * | ||
| * The clearing of weakrefs is suble and must be done carefully, as there was | ||
| * previous bugs related to this. First, weakrefs to the unreachable set of | ||
| * objects must be cleared before we start calling `tp_clear`. If we don't, | ||
| * those weakrefs can reveal unreachable objects to Python-level code and that | ||
| * is not safe. Many objects are not usable after `tp_clear` has been called | ||
| * and could even cause crashes if accessed (see bpo-38006 and gh-91636 as | ||
| * example bugs). | ||
| * | ||
| * Weakrefs with callbacks always need to be cleared before executing the | ||
| * callback. That's because sometimes a callback will call the ref object, | ||
| * to check if the reference is actually dead (KeyedRef does this, for | ||
| * example). We want to indicate that it is dead, even though it is possible | ||
| * a finalizer might resurrect it. Clearing also prevents the callback from | ||
| * executing more than once. | ||
| * | ||
| * Since Python 2.3, all weakrefs to cyclic garbage have been cleared *before* | ||
| * calling finalizers. However, since tp_subclasses started being necessary | ||
| * to invalidate caches (e.g. by PyType_Modified), that clearing has created | ||
| * a bug. If the weakref to the subclass is cleared before a finalizer is | ||
| * called, the cache may not be correctly invalidated. That can lead to | ||
| * segfaults since the caches can refer to deallocated objects (GH-91636 | ||
| * is an example). Now, we delay the clear of weakrefs without callbacks | ||
| * until *after* finalizers have been executed. That means weakrefs without | ||
| * callbacks are still usable while finalizers are being executed. | ||
| * | ||
| * The weakrefs that are inside the unreachable set must also be cleared. | ||
| * The reason this is required is not immediately obvious. If the weakref | ||
| * refers to an object outside of the unreachable set, that object might go | ||
| * away when we start clearing objects. Normally, the object should also be | ||
| * part of the unreachable set but that's not true in the case of incomplete | ||
| * or missing `tp_traverse` methods. When that object goes away, the callback | ||
| * for weakref can be executed and that could reveal unreachable objects to | ||
| * Python-level code. See bpo-38006 as an example bug. | ||
| */ | ||
| static int | ||
| handle_weakref_callbacks(PyGC_Head *unreachable, PyGC_Head *old) | ||
|
|
@@ -904,36 +938,19 @@ handle_weakref_callbacks(PyGC_Head *unreachable, PyGC_Head *old) | |
| // next pointer in the wrlist). | ||
| next_wr = wr->wr_next; | ||
|
|
||
| // Weakrefs with callbacks always need to be cleared before | ||
| // executing the callback. Sometimes the callback will call | ||
| // the ref object, to check if it's actually a dead reference | ||
| // (KeyedRef does this, for example). We want to indicate that it | ||
| // is dead, even though it is possible a finalizer might resurrect | ||
| // it. Clearing also prevents the callback from being executing | ||
| // more than once. | ||
| // | ||
| // Since Python 2.3, all weakrefs to cyclic garbage have | ||
| // been cleared *before* calling finalizers. However, since | ||
| // tp_subclasses started being necessary to invalidate caches | ||
| // (e.g. by PyType_Modified()), that clearing has created a bug. | ||
| // If the weakref to the subclass is cleared before a finalizer | ||
| // is called, the cache may not be correctly invalidated. That | ||
| // can lead to segfaults since the caches can refer to deallocated | ||
| // objects. Delaying the clear of weakrefs until *after* | ||
| // finalizers have been called fixes that bug. | ||
| if (wr->wr_callback != NULL) { | ||
| // _PyWeakref_ClearRef clears the weakref but leaves the | ||
| // callback pointer intact. Obscure: it also changes *wrlist. | ||
| _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op); | ||
| _PyWeakref_ClearRef(wr); | ||
| _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None); | ||
| } | ||
|
|
||
| if (wr->wr_callback == NULL) { | ||
|
nascheme marked this conversation as resolved.
|
||
| /* no callback */ | ||
| continue; | ||
| } | ||
|
|
||
| // Clear the weakref. See the comments above this function for | ||
| // reasons why we need to clear weakrefs that have callbacks. | ||
| // Note that _PyWeakref_ClearRef clears the weakref but leaves the | ||
| // callback pointer intact. Obscure: it also changes *wrlist. | ||
| _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op); | ||
| _PyWeakref_ClearRef(wr); | ||
| _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None); | ||
|
|
||
| /* Headache time. `op` is going away, and is weakly referenced by | ||
| * `wr`, which has a callback. Should the callback be invoked? If wr | ||
| * is also trash, no: | ||
|
|
@@ -1030,7 +1047,8 @@ handle_weakref_callbacks(PyGC_Head *unreachable, PyGC_Head *old) | |
| } | ||
|
|
||
| /* Clear all weakrefs to unreachable objects. When this returns, no object in | ||
| * `unreachable` is weakly referenced anymore. | ||
| * `unreachable` is weakly referenced anymore. See the comments above | ||
| * handle_weakref_callbacks() for why these weakrefs need to be cleared. | ||
| */ | ||
| static void | ||
| clear_weakrefs(PyGC_Head *unreachable) | ||
|
|
@@ -1045,16 +1063,9 @@ clear_weakrefs(PyGC_Head *unreachable) | |
| next = GC_NEXT(gc); | ||
|
|
||
| if (PyWeakref_Check(op)) { | ||
| /* A weakref inside the unreachable set must be cleared. If we | ||
| * allow its callback to execute inside delete_garbage(), it | ||
| * could expose objects that have tp_clear already called on | ||
| * them. Or, it could resurrect unreachable objects. One way | ||
| * this can happen is if some container objects do not implement | ||
| * tp_traverse. Then, wr_object can be outside the unreachable | ||
| * set but can be deallocated as a result of breaking the | ||
| * reference cycle. If we don't clear the weakref, the callback | ||
| * will run and potentially cause a crash. See bpo-38006 for | ||
| * one example. | ||
| /* A weakref inside the unreachable set is always cleared. See | ||
| * the comments above handle_weakref_callbacks() for why these | ||
| * must be cleared. | ||
| */ | ||
| _PyWeakref_ClearRef((PyWeakReference *)op); | ||
| } | ||
|
|
@@ -1790,19 +1801,8 @@ gc_collect_region(PyThreadState *tstate, | |
| gc_list_init(&final_unreachable); | ||
| handle_resurrected_objects(&unreachable, &final_unreachable, to); | ||
|
|
||
| /* 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 if those weakrefs were not cleared, that could reveal unreachable | ||
| * objects. | ||
| * | ||
| * We used to clear weakrefs earlier, before calling finalizers. That | ||
| * causes at least two problems. First, the finalizers could create | ||
| * new weakrefs, that refer to unreachable objects. Those would not be | ||
| * cleared and could cause the problem described above (see GH-91636 as | ||
| * an example). Second, we need the weakrefs in the tp_subclasses to | ||
| * *not* be cleared so that caches based on the type version are correctly | ||
| * invalidated (see GH-135552 as a bug caused by this). | ||
| /* Clear weakrefs to objects in the unreachable set. See the comments | ||
| * above handle_weakref_callbacks() for details. | ||
| */ | ||
| clear_weakrefs(&final_unreachable); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ough! Nice trick with |
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.