[2.7] bpo-35368: Make PyMem_Malloc() thread-safe in debug mode by vstinner · Pull Request #10828 · python/cpython (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation4 Commits1 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

vstinner

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

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?

@vstinner

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

benjaminp

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

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