Skip to content

gh-129185: Simplify PyTraceMalloc_Track() #129256

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

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,7 @@ _Py_Finalize(_PyRuntimeState *runtime)

/* Disable tracemalloc after all Python objects have been destroyed,
so it is possible to use tracemalloc in objects destructor. */
_PyTraceMalloc_Stop();
_PyTraceMalloc_Fini();

/* Finalize any remaining import state */
// XXX Move these up to where finalize_modules() is currently.
Expand Down Expand Up @@ -2166,7 +2166,6 @@ _Py_Finalize(_PyRuntimeState *runtime)

finalize_interp_clear(tstate);

_PyTraceMalloc_Fini();

#ifdef Py_TRACE_REFS
/* Display addresses (& refcnts) of all objects still alive.
Expand Down
26 changes: 2 additions & 24 deletions Python/tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1203,17 +1203,9 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
size_t size)
{
PyGILState_STATE gil_state = PyGILState_Ensure();
int result;

// gh-129185: Check before TABLES_LOCK() to support calls after
// _PyTraceMalloc_Fini().
if (!tracemalloc_config.tracing) {
result = -2;
goto done;
}

TABLES_LOCK();

int result;
if (tracemalloc_config.tracing) {
result = tracemalloc_add_trace_unlocked(domain, ptr, size);
}
Expand All @@ -1223,29 +1215,17 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
}

TABLES_UNLOCK();
done:
PyGILState_Release(gil_state);

return result;
}


int
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
{
// Need the GIL to prevent races on the first 'tracing' test
PyGILState_STATE gil_state = PyGILState_Ensure();
int result;

// gh-129185: Check before TABLES_LOCK() to support calls after
// _PyTraceMalloc_Fini()
if (!tracemalloc_config.tracing) {
result = -2;
goto done;
}

TABLES_LOCK();

int result;
if (tracemalloc_config.tracing) {
tracemalloc_remove_trace_unlocked(domain, ptr);
result = 0;
Expand All @@ -1256,8 +1236,6 @@ PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
}

TABLES_UNLOCK();
done:
PyGILState_Release(gil_state);
return result;
}

Expand Down
Loading