Skip to content
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

GH-114695: Add sys._clear_internal_caches #115152

Merged
merged 20 commits into from
Feb 12, 2024

Conversation

brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented Feb 8, 2024

This new function immediately releases all tier-two related references and memory, which is useful for refleak-hunting.

Currently, invalid executors need to be hit again in order to be cleared (which may never happen), and there is no way to free the code object's executor array. This PR adds a (weak) pointer from executors back to the code object they're installed in, which allows them to immediately remove themselves upon invalidation. Code objects can clear this pointer if the executor outlives them.

This PR also changes the linked list to contain every valid executor, and no invalid ones. That means that the old linked member is redundant with the valid field, so it's removed.


📚 Documentation preview 📚: https://cpython-previews--115152.org.readthedocs.build/

@brandtbucher brandtbucher added type-bug An unexpected behavior, bug, or error tests Tests in the Lib/test dir interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Feb 8, 2024
@brandtbucher brandtbucher self-assigned this Feb 8, 2024
Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good, but I have a couple of concerns.

  1. I don't like exposing low-level implementation details like executors though, so could we rename sys._clear_executors()?
    Maybe merge it with sys._clear_type_cache() into something like sys._clear_internal_caches()?
    That way we can add other caches, jitted code, etc. and clear them all without changing the API.

  2. We might want to invalidate a lot of executors in a stop-the-world event. Merely setting the valid flag to zero (as we do now) is fine, but freeing lots of object may not be. Can we go back to invalidating executors not doing much work, and leaving clean up to later?

_PyBloomFilter bloom;
_PyExecutorLinkListNode links;
PyCodeObject *code; // Weak (NULL if no corresponding ENTER_EXECUTOR).
int index; // Index of ENTER_EXECUTOR in the above code object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this above the bloom filter, to avoid holes?

exec->vm_data.valid = false;
exec->vm_data.linked = false;
exec = next;
_PyExecutorObject *executor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for moving the declaration out of the loop?

Copy link
Member Author

@brandtbucher brandtbucher Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a while loop now.

Would you prefer to leave it as a for loop? It's a tiny bit more repetitive, but maybe easier to follow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The for loop is nice as it limits the scope of the iteration variable. It doesn't matter much.

@brandtbucher brandtbucher changed the title GH-114695: Add sys._clear_executors GH-114695: Add sys._clear_internal_caches Feb 10, 2024
@brandtbucher brandtbucher requested a review from a team as a code owner February 10, 2024 01:29
@offensive-vk
Copy link

I'm Sorry, mistakenly clicked on review button.
Sorry for the inconvenience.

@markshannon markshannon merged commit 235cacf into python:main Feb 12, 2024
60 checks passed
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this pull request Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants