Get rid of use-after-free in async registry #21779
Merged
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.
Use-after-free happened in nightly alubsan run.
The mark for deletion of a node after all its references are gone was actually much too complicated: I wanted to make sure that if there is a hierarchy of parents where each parent has only one child and the lowest child is marked for deletion like this:
all parents in the hierarchy are directly marked for deletion as well and we don't have to run the garbage collection several times to do that (A node has a shared pointer to its parent: when the shared pointer to the parent is destroyed - normally happening in the garbage collection - the node is marked for deletion). In the old behaviour, we cascaded through the full hierarchy and marked parents for deletion as well. Then at some point the shared pointer to the parent is also destroyed, leading to another try to access the parent node. If a garbage collection ran in between, the node was already deleted, leading to a use-after-free.
This easy solution: Get rid of the parent reference when marking the node for deletion. Then the shared pointer to the parent is potentially destroyed earlier, leading to a mark for deletion of the parent automatically. Don't know why I did not thought about this earlier...