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-112723: Call PyThreadState_Clear() from the correct interpreter #112776

Merged
merged 1 commit into from
Dec 13, 2023

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Dec 5, 2023

The PyThreadState_Clear() function must only be called with the GIL held and must be called from the same interpreter as the passed in thread state. Otherwise, any Python objects on the thread state may be destroyed using the wrong interpreter, leading to memory corruption.

This is also important for Py_GIL_DISABLED builds because object free lists will be associated with PyThreadStates and cleared in PyThreadState_Clear().

This fixes two places that called PyThreadState_Clear() from the wrong interpreter and adds an assertion to PyThreadState_Clear().

The new_interpreter() function required some refactoring so that no calls that may return errors happen between the creation of the thread state and the acquisition of the GIL.

…reter

The `PyThreadState_Clear()` function must only be called with the GIL
held and must be called from the same interpreter as the passed in
thread state. Otherwise, any Python objects on the thread state may be
destroyed using the wrong interpreter, leading to memory corruption.

This is also important for `Py_GIL_DISABLED` builds because free lists
will be associated with PyThreadStates and cleared in
`PyThreadState_Clear()`.

This fixes two places that called `PyThreadState_Clear()` from the wrong
interpreter and adds an assertion to `PyThreadState_Clear()`.
@colesbury
Copy link
Contributor Author

cc @ericsnowcurrently

Modules/_xxsubinterpretersmodule.c Show resolved Hide resolved
Python/pylifecycle.c Show resolved Hide resolved
@carljm carljm merged commit a3c0318 into python:main Dec 13, 2023
35 checks passed
@colesbury colesbury deleted the PyThreadState_Clear branch January 5, 2024 18:37
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…reter (python#112776)

The `PyThreadState_Clear()` function must only be called with the GIL
held and must be called from the same interpreter as the passed in
thread state. Otherwise, any Python objects on the thread state may be
destroyed using the wrong interpreter, leading to memory corruption.

This is also important for `Py_GIL_DISABLED` builds because free lists
will be associated with PyThreadStates and cleared in
`PyThreadState_Clear()`.

This fixes two places that called `PyThreadState_Clear()` from the wrong
interpreter and adds an assertion to `PyThreadState_Clear()`.
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…reter (python#112776)

The `PyThreadState_Clear()` function must only be called with the GIL
held and must be called from the same interpreter as the passed in
thread state. Otherwise, any Python objects on the thread state may be
destroyed using the wrong interpreter, leading to memory corruption.

This is also important for `Py_GIL_DISABLED` builds because free lists
will be associated with PyThreadStates and cleared in
`PyThreadState_Clear()`.

This fixes two places that called `PyThreadState_Clear()` from the wrong
interpreter and adds an assertion to `PyThreadState_Clear()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants