gh-156228: Use PyMem_Raw* allocators for OpenSSL in _ssl and _hashlib - #156229
gh-156228: Use PyMem_Raw* allocators for OpenSSL in _ssl and _hashlib#156229kumaraditya303 wants to merge 1 commit into
Conversation
…ashlib Install OpenSSL memory hooks with CRYPTO_set_mem_functions() when _ssl or _hashlib is loaded, routing OpenSSL allocations through the raw memory allocators so they are visible to tracemalloc and custom allocators. Installation fails benignly if another libcrypto user allocated first.
|
I'd like to merge this by weekend if there are no objections, I've posted the performance analysis on the issue. |
| allocations through the Python raw memory allocators, making OpenSSL memory | ||
| usage visible to :mod:`tracemalloc` and to custom allocators installed with |
There was a problem hiding this comment.
I'm not sure we can do this safely. tracemalloc_alloc calls PyGILState_Ensure() within PyMem_Raw allocations, OpenSSL allocates memory with its own internal locks held, and we call into OpenSSL in many circumstances with the GIL released.
There was a problem hiding this comment.
There is ongoing discussion on tracemalloc regarding this in #155725 but I don't think it causes deadlocks, in worst case it causes contention but that is the cost of using tracemalloc.
There was a problem hiding this comment.
If we were always consistent about releasing the GIL when calling into openssl APIs across _ssl and _hashlib it should avoid deadlocks... but I don't think we are.
And other extension modules calling libssl APIs might not and we cannot control that. So I don't think we can do this at all.
I let Claude hunt for an example where we don't... its found one and written a deadlock reproducer when using our PyMem_Raw allocators with OpenSSL - deadlocks on regular and free-threaded builds. Reproducer with explanation gist.
ssl.create_default_context()sha3_512contextPyMem_Raw*allocator for OpenSSL in_ssland_hashlib#156228