Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Jan 23, 2025

@vstinner
Copy link
Member Author

cc @ZeroIntensity

#define tables_lock _PyRuntime.tracemalloc.tables_lock
#define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
#define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
#define TABLES_LOCK() PyMutex_LockFlags(&tables_lock, _Py_LOCK_DONT_DETACH)
Copy link
Member Author

@vstinner vstinner Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyMutex_Lock(&tables_lock) cannot be used "free" allocator hooks which don't acquire the GIL.

Maybe PyMutex_Lock(&tables_lock) should be preferred in functions which hold the GIL?

Copy link
Member

@ZeroIntensity ZeroIntensity Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it matters. TABLES_LOCK() didn't detach the thread state before, no need to now. (In fact, some unpredictable things might happen if another thread takes the GIL and then uses tracemalloc.)

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. We should also consider using _Py_atomic for some fields (especially tracing).

@vstinner vstinner merged commit c005ea4 into python:main Jan 24, 2025
44 checks passed
@vstinner vstinner deleted the tracemalloc_pymutex branch January 24, 2025 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants