gh-123022: Fix crash with Py_Initialize in background thread (#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.
This commit is contained in:
Sam Gross 2024-08-17 16:04:08 -04:00 committed by GitHub
parent 40632b1f1d
commit d061ffea7b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 40 additions and 5 deletions

View file

@ -8,6 +8,7 @@
#include <Python.h>
#include "pycore_initconfig.h" // _PyConfig_InitCompatConfig()
#include "pycore_runtime.h" // _PyRuntime
#include "pycore_pythread.h" // PyThread_start_joinable_thread()
#include "pycore_import.h" // _PyImport_FrozenBootstrap
#include <inttypes.h>
#include <stdio.h>
@ -2022,6 +2023,22 @@ static int test_init_main_interpreter_settings(void)
return 0;
}
static void do_init(void *unused)
{
_testembed_Py_Initialize();
Py_Finalize();
}
static int test_init_in_background_thread(void)
{
PyThread_handle_t handle;
PyThread_ident_t ident;
if (PyThread_start_joinable_thread(&do_init, NULL, &ident, &handle) < 0) {
return -1;
}
return PyThread_join_thread(handle);
}
#ifndef MS_WINDOWS
#include "test_frozenmain.h" // M_test_frozenmain
@ -2211,6 +2228,7 @@ static struct TestCase TestCases[] = {
{"test_get_argc_argv", test_get_argc_argv},
{"test_init_use_frozen_modules", test_init_use_frozen_modules},
{"test_init_main_interpreter_settings", test_init_main_interpreter_settings},
{"test_init_in_background_thread", test_init_in_background_thread},
// Audit
{"test_open_code_hook", test_open_code_hook},