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-123022: Fix crash with Py_Initialize in background thread #123052

Merged
merged 1 commit into from
Aug 17, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Aug 15, 2024

Check that the current default heap is initialized in _mi_os_get_aligned_hint and mi_os_claim_huge_pages.

The mimalloc function _mi_os_get_aligned_hint assumes that there is an initialized default heap. This is true for our main thread, but not for background threads. The problematic code path is usually called during initialization (i.e., Py_Initialize), but it may also be called if the program allocates large amounts of memory in total.

The crash only affected the free-threaded build.

Check that the current default heap is initialized in
`_mi_os_get_aligned_hint` and `mi_os_claim_huge_pages`.

The mimalloc function `_mi_os_get_aligned_hint` assumes that there is an
initialized default heap. This is true for our main thread, but not for
background threads. The problematic code path is usually called during
initialization (i.e., `Py_Initialize`), but it may also be called if the
program allocates large amounts of memory in total.

The crash only affected the free-threaded build.
@colesbury
Copy link
Contributor Author

We may want to consider setting the default heap in the future, but that would be a more involved change because CPython's thread destruction process doesn't line up perfectly with mimalloc's model. In particular, PyThreadState instances may be destroyed by different threads, while mimalloc assumes that the thread's default heap is destroyed when the OS thread exits.

The approach in this PR -- guarding the call to _mi_heap_random -- seemed like it would have the least risk of complications.

@colesbury colesbury marked this pull request as ready for review August 15, 2024 20:19
@colesbury
Copy link
Contributor Author

@Yhg1s - I think it's worth considering this for 3.13rc2. Let me know what you think.

@Yhg1s
Copy link
Member

Yhg1s commented Aug 17, 2024

@Yhg1s - I think it's worth considering this for 3.13rc2. Let me know what you think.

Yeah, this should go into rc2.

@colesbury colesbury merged commit d061ffe into python:main Aug 17, 2024
42 checks passed
@colesbury colesbury deleted the gh-123022-mi-prim_get_default branch August 17, 2024 20:04
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 17, 2024
…ythonGH-123052)

Check that the current default heap is initialized in
`_mi_os_get_aligned_hint` and `mi_os_claim_huge_pages`.

The mimalloc function `_mi_os_get_aligned_hint` assumes that there is an
initialized default heap. This is true for our main thread, but not for
background threads. The problematic code path is usually called during
initialization (i.e., `Py_Initialize`), but it may also be called if the
program allocates large amounts of memory in total.

The crash only affected the free-threaded build.
(cherry picked from commit d061ffe)

Co-authored-by: Sam Gross <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Aug 17, 2024

GH-123114 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Aug 17, 2024
colesbury added a commit that referenced this pull request Aug 17, 2024
…GH-123052) (#123114)

Check that the current default heap is initialized in
`_mi_os_get_aligned_hint` and `mi_os_claim_huge_pages`.

The mimalloc function `_mi_os_get_aligned_hint` assumes that there is an
initialized default heap. This is true for our main thread, but not for
background threads. The problematic code path is usually called during
initialization (i.e., `Py_Initialize`), but it may also be called if the
program allocates large amounts of memory in total.

The crash only affected the free-threaded build.
(cherry picked from commit d061ffe)

Co-authored-by: Sam Gross <[email protected]>
jeremyhylton pushed a commit to jeremyhylton/cpython that referenced this pull request Aug 19, 2024
…ython#123052)

Check that the current default heap is initialized in
`_mi_os_get_aligned_hint` and `mi_os_claim_huge_pages`.

The mimalloc function `_mi_os_get_aligned_hint` assumes that there is an
initialized default heap. This is true for our main thread, but not for
background threads. The problematic code path is usually called during
initialization (i.e., `Py_Initialize`), but it may also be called if the
program allocates large amounts of memory in total.

The crash only affected the free-threaded build.
blhsing pushed a commit to blhsing/cpython that referenced this pull request Aug 22, 2024
…ython#123052)

Check that the current default heap is initialized in
`_mi_os_get_aligned_hint` and `mi_os_claim_huge_pages`.

The mimalloc function `_mi_os_get_aligned_hint` assumes that there is an
initialized default heap. This is true for our main thread, but not for
background threads. The problematic code path is usually called during
initialization (i.e., `Py_Initialize`), but it may also be called if the
program allocates large amounts of memory in total.

The crash only affected the free-threaded build.
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