Skip to content

Commit 026cd39

Browse files
committed
pythongh-160267: Keep thread and interpreter state memory alive for remote profilers
1 parent ff042f1 commit 026cd39

3 files changed

Lines changed: 73 additions & 11 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a crash when :mod:`profiling.sampling` profiles a process that creates and
2+
destroys threads or subinterpreters quickly. Memory of deleted thread and
3+
interpreter states is now reused for new states instead of being freed until
4+
the runtime is finalized.

‎Python/pystate.c‎

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,20 @@ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
309309
static const _PyRuntimeState initial = _PyRuntimeState_INIT(_PyRuntime, "");
310310
_Py_COMP_DIAG_POP
311311

312+
/* Remote profilers access interpreter and thread states without synchronizing
313+
with their lifetime. Until the runtime is finalized, keep deleted states in
314+
per-type free lists, so that a stale access only reaches a state of the same
315+
type. Each state is preceded by the free-list link and the block to free. */
316+
static struct {
317+
PyMutex mutex;
318+
void *tstates;
319+
void *interpreters;
320+
} state_freelists;
321+
312322
#define LOCKS_INIT(runtime) \
313323
{ \
314324
&(runtime)->interpreters.mutex, \
325+
&state_freelists.mutex, \
315326
&(runtime)->xi.data_lookup.registry.mutex, \
316327
&(runtime)->unicode_state.ids.mutex, \
317328
&(runtime)->imports.extensions.mutex, \
@@ -390,6 +401,19 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
390401
assert(runtime->object_state.interpreter_leaks == 0);
391402
#endif
392403
gilstate_clear();
404+
405+
void **lists[] = {&state_freelists.tstates, &state_freelists.interpreters};
406+
for (size_t i = 0; i < Py_ARRAY_LENGTH(lists); i++) {
407+
PyMutex_LockFlags(&state_freelists.mutex, _Py_LOCK_DONT_DETACH);
408+
void **entry = *lists[i];
409+
*lists[i] = NULL;
410+
PyMutex_Unlock(&state_freelists.mutex);
411+
while (entry != NULL) {
412+
void **next = entry[0];
413+
PyMem_RawFree(entry[1]);
414+
entry = next;
415+
}
416+
}
393417
}
394418

395419
#ifdef HAVE_FORK
@@ -445,24 +469,50 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime)
445469
return _PyStatus_OK();
446470
}
447471

448-
static PyInterpreterState *
449-
alloc_interpreter(void)
472+
static void *
473+
alloc_state(void **freelist, size_t size, size_t alignment)
450474
{
451-
// Aligned allocation for PyInterpreterState.
452-
// the first word of the memory block is used to store
453-
// the original pointer to be used later to free the memory.
454-
size_t alignment = _Alignof(PyInterpreterState);
455-
size_t allocsize = sizeof(PyInterpreterState) + sizeof(void *) + alignment - 1;
475+
PyMutex_LockFlags(&state_freelists.mutex, _Py_LOCK_DONT_DETACH);
476+
void **entry = *freelist;
477+
if (entry != NULL) {
478+
*freelist = *entry;
479+
}
480+
PyMutex_Unlock(&state_freelists.mutex);
481+
if (entry != NULL) {
482+
return entry + 2;
483+
}
484+
485+
size_t allocsize = size + 2 * sizeof(void *) + alignment - 1;
456486
void *mem = PyMem_RawCalloc(1, allocsize);
457487
if (mem == NULL) {
458488
return NULL;
459489
}
460-
void *ptr = _Py_ALIGN_UP((char *)mem + sizeof(void *), alignment);
490+
void *ptr = _Py_ALIGN_UP((char *)mem + 2 * sizeof(void *), alignment);
461491
((void **)ptr)[-1] = mem;
462492
assert(_Py_IS_ALIGNED(ptr, alignment));
463493
return ptr;
464494
}
465495

496+
static void
497+
recycle_state(void **freelist, void *state, size_t size)
498+
{
499+
memset(state, 0, size);
500+
void **entry = (void **)state - 2;
501+
502+
PyMutex_LockFlags(&state_freelists.mutex, _Py_LOCK_DONT_DETACH);
503+
entry[0] = *freelist;
504+
// Keep the list consistent for a child forked by another thread.
505+
_Py_atomic_store_ptr_release(freelist, entry);
506+
PyMutex_Unlock(&state_freelists.mutex);
507+
}
508+
509+
static PyInterpreterState *
510+
alloc_interpreter(void)
511+
{
512+
return alloc_state(&state_freelists.interpreters, sizeof(PyInterpreterState),
513+
_Alignof(PyInterpreterState));
514+
}
515+
466516
static void
467517
free_interpreter(PyInterpreterState *interp)
468518
{
@@ -491,7 +541,13 @@ free_interpreter(PyInterpreterState *interp)
491541
interp->obmalloc = NULL;
492542
}
493543
assert(_Py_IS_ALIGNED(interp, _Alignof(PyInterpreterState)));
494-
PyMem_RawFree(((void **)interp)[-1]);
544+
// A daemon thread may still own the embedded initial thread state.
545+
if (interp->_initialized
546+
&& _Py_atomic_load_ptr(&interp->threads.preallocated) == NULL)
547+
{
548+
return;
549+
}
550+
recycle_state(&state_freelists.interpreters, interp, sizeof(*interp));
495551
}
496552
}
497553

@@ -1517,7 +1573,8 @@ alloc_threadstate(PyInterpreterState *interp)
15171573

15181574
// Fall back to the allocator.
15191575
if (tstate == NULL) {
1520-
tstate = PyMem_RawCalloc(1, sizeof(_PyThreadStateImpl));
1576+
tstate = alloc_state(&state_freelists.tstates, sizeof(*tstate),
1577+
_Alignof(_PyThreadStateImpl));
15211578
if (tstate == NULL) {
15221579
return NULL;
15231580
}
@@ -1544,7 +1601,7 @@ free_threadstate(_PyThreadStateImpl *tstate)
15441601
_Py_atomic_store_ptr(&interp->threads.preallocated, tstate);
15451602
}
15461603
else {
1547-
PyMem_RawFree(tstate);
1604+
recycle_state(&state_freelists.tstates, tstate, sizeof(*tstate));
15481605
}
15491606
}
15501607

‎Tools/c-analyzer/cpython/ignored.tsv‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Python/pyhash.c - _Py_HashSecret -
5656

5757
## thread-safe hashtable (internal locks)
5858
Python/parking_lot.c - buckets -
59+
Python/pystate.c - state_freelists -
5960

6061
## shared empty sentinel for the per-type method cache
6162
Python/typecache.c - empty_cache -

0 commit comments

Comments
 (0)