Skip to content

[2.7] bpo-35368: Make PyMem_Malloc() thread-safe in debug mode #10828

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
Dec 3, 2018
Merged

[2.7] bpo-35368: Make PyMem_Malloc() thread-safe in debug mode #10828

merged 1 commit into from
Dec 3, 2018

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Nov 30, 2018

When Python is compiled in debug mode, PyMem_Malloc() uses debug
hooks, but it also uses pymalloc allocator instead of malloc().
Problem: pymalloc is not thread-safe, whereas PyMem_Malloc() is
thread-safe in release mode (it's a thin wrapper to malloc() in this
case).

Modify the debug hook to use malloc() for PyMem_Malloc().

https://bugs.python.org/issue35368

@vstinner
Copy link
Member Author

I wrote this PR because I used PyMem_Malloc()/PyMem_Free() in PyThread_start_new_thread() whereas PyMem_Malloc()/PyMem_Free() are not thread safe when Python is build is debug mode and I didn't know that!
https://bugs.python.org/issue33015#msg330806

But I fixed PyThread_start_new_thread() using PR #10829 (commit bc9f53f). So this change is not strictly needed.

@serhiy-storchaka, @benjaminp: Is it too late to fix PyMem_Malloc() to make it thread-safe in Python 2.7?

When Python is compiled in debug mode, PyMem_Malloc() uses debug
hooks, but it also uses pymalloc allocator instead of malloc().
Problem: pymalloc is not thread-safe, whereas PyMem_Malloc() is
thread-safe in release mode (it's a thin wrapper to malloc() in this
case).

Modify the debug hook to use malloc() for PyMem_Malloc().
@vstinner vstinner changed the title [2.7] bpo-33015: Make PyMem_Malloc() thread-safe in debug mode [2.7] bpo-35368: Make PyMem_Malloc() thread-safe in debug mode Nov 30, 2018
@vstinner
Copy link
Member Author

I created a new issue for this change, as asked by @gpshead: https://bugs.python.org/issue35368

Copy link
Contributor

@benjaminp benjaminp left a comment

Choose a reason for hiding this comment

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

Debug mode changes are probably okay.

@vstinner vstinner merged commit c275be5 into python:2.7 Dec 3, 2018
@vstinner vstinner deleted the pymem_threadsafe branch December 3, 2018 11:29
@vstinner
Copy link
Member Author

vstinner commented Dec 3, 2018

Thanks for the review @benjaminp! Hopefully, I don't think that anyone uses PyMem_Malloc() without holding the GIL in Python 2 ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants