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
Release the TLS lock during allocations #53996
Comments
Holding the "keymutex" lock during malloc and free operations is not a good idea. The reason is, that custom implementations of malloc and free, can use the TLS themselves. This is, for example, true in embedded situations, where one wants to replace malloc with, e.g. appMalloc, (to monitor the memory useage of Python) and appMalloc itself uses python TLS to find the current python State. This change makes the malloc and free calls outside the lock. The change in PyThread_set_key_value, requiring an extra lock allocate, has no significant performance impact since this is a rare api. |
You have a bug in PyThread_delete_key_value() (to_free = NULL?). |
You're right. |
What is appMalloc, and what does it have to do with some Python lock? You seem to suggest that some malloc implementations make use of Python interpreter internals. I would call that a bug in the malloc implementation (it violates standard layering assumptions), and so I'm -1 on inclusion of this patch. |
You may find this hard to believe, but we do in fact embed python into other applications. In this case, it is UnrealEngine, to drive a complex, console based game. Yes, embedding python is much harder than it need be and I'll submit some patches to make that easier someday, but that's not the point of this. appMalloc, is in this case, the canonical memory allocator in UnrealEngine. But it could be any other memory allocator so that is beside the point. The problem at hand, however, is this memory allocator _may_ have to inquire about the state of Python. It would do this, for example, to gather statistics about Python's memory use. This is critically important when developing console based applications, where every Kilobyte counts. Embedding python sometimes requires the replacement of the standard libc malloc with something else. What appMalloc is doing, in this case, is for every allocation, to get the python TLS pointer. There is nothing wrong with this, this is a GIL free operation, and it will return either NULL or the current TLS. And it works, except, when appMalloc (through python's malloc) is being invoked from the TLS entry creation mechanism itself. Then it deadlocks. Now, regardless of the above, surely it is an improvement in general if we make tighter use of the TLS lock. It's not a good idea to hold this lock across malloc calls if we can avoid it. The patch is harmless, might even be an improvement, so why object to it? It removes yet another "gotcha" that embedders, or those replacing malloc, (or instrumenting python's malloc use) have to face. Cheers, K |
I forgot to add: The API that our (instrumented) malloc implementation is calling is:
PyGILState_GetThisThreadState(); |
This is actually very easy to believe.
This seems to be the core of the issue. Any other memory allocator
I find this wrong. It violates the software layering. The memory
The code change itself is harmless, yes. The comment is not. It imposes So I remain -1 on this change. |
Of course it does, if it you want to have any hope of instrumenting your python memory usage with detailed python runtime information. Your statement islike saying: "A profiler has no business looking at the thread callstack." Note that we are not making any new requirements on python here. Merely facilitating the process, for those implementations that _wish_ to do so (at their own risk.) So, although you have nothing against the patch as such, you are against it on the principle that I am using it to facilitate something that you disapprove of. I find that a quite unreasonable position. |
But you are. So far, there was no guarantee whatsoever about the state
I think it's harmless - I don't think it is a good patch.
No. It's not the usage that I disapprove but, but the new requirement |
I'll rework this for python 3.x and see where that gets us. |
New patch, based on the cpython tip. |
hm, for some reason this patch isn't viewable in side-by-side |
Making this low priority since it applies only to platforms without Windows and pthread support. |
Closing this since it applies only to our custom tls implementation. Most platforms use native tls now. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: