gh-130421: Fix data race on timebase initialization (gh-130592)

Windows and macOS require precomputing a "timebase" in order to convert
OS timestamps into nanoseconds. Retrieve and compute this value during
runtime initialization to avoid data races when accessing the time.
This commit is contained in:
Sam Gross 2025-02-27 08:27:54 -05:00 committed by GitHub
parent 45a24f54af
commit d027787c8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 48 deletions

View file

@ -20,6 +20,7 @@
#include "pycore_pystate.h"
#include "pycore_runtime_init.h" // _PyRuntimeState_INIT
#include "pycore_stackref.h" // Py_STACKREF_DEBUG
#include "pycore_time.h" // _PyTime_Init()
#include "pycore_obmalloc.h" // _PyMem_obmalloc_state_on_heap()
#include "pycore_uniqueid.h" // _PyObject_FinalizePerThreadRefcounts()
@ -461,6 +462,11 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
assert(!runtime->_initialized);
}
PyStatus status = _PyTime_Init(&runtime->time);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
if (gilstate_tss_init(runtime) != 0) {
_PyRuntimeState_Fini(runtime);
return _PyStatus_NO_MEMORY();