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

refactor(profiling): add more GIL assertions to memalloc #12000

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion ddtrace/profiling/collector/_memalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,19 @@ memalloc_init()
}

static void
memalloc_add_event(memalloc_context_t* ctx, void* ptr, size_t size)
memalloc_assert_gil()
{
if (g_crash_on_no_gil && !PyGILState_Check()) {
int* p = NULL;
*p = 0;
abort(); // should never reach here
}
}

static void
memalloc_add_event(memalloc_context_t* ctx, void* ptr, size_t size)
{
memalloc_assert_gil();

uint64_t alloc_count = atomic_add_clamped(&global_alloc_tracker->alloc_count, 1, ALLOC_TRACKER_MAX_COUNT);

Expand Down Expand Up @@ -332,6 +338,8 @@ memalloc_stop(PyObject* Py_UNUSED(module), PyObject* Py_UNUSED(args))
return NULL;
}

memalloc_assert_gil();

PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &global_memalloc_ctx.pymem_allocator_obj);
memalloc_tb_deinit();
if (memlock_trylock(&g_memalloc_lock)) {
Expand Down Expand Up @@ -389,6 +397,8 @@ iterevents_new(PyTypeObject* type, PyObject* Py_UNUSED(args), PyObject* Py_UNUSE
if (!iestate)
return NULL;

memalloc_assert_gil();

/* reset the current traceback list */
if (memlock_trylock(&g_memalloc_lock)) {
iestate->alloc_tracker = global_alloc_tracker;
Expand Down
Loading