classification
Title: Release the TLS lock during allocations
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: kristjan.jonsson, loewis, pitrou
Priority: low Keywords: patch

Created on 2010-09-07 06:03 by kristjan.jonsson, last changed 2012-03-31 11:38 by kristjan.jonsson. This issue is now closed.

Files
File name Uploaded Description Edit
tlspatch.patch kristjan.jonsson, 2010-09-07 06:03 review
tlspatch.patch kristjan.jonsson, 2010-09-08 05:09 review
tlspatch.patch kristjan.jonsson, 2012-03-20 14:22 review
Messages (14)
msg115743 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2010-09-07 06:03
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.
msg115763 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-07 14:17
You have a bug in PyThread_delete_key_value() (to_free = NULL?).
Also, you should move the "/* NB This does *not* free p->value! */" comments at the right places.
msg115848 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2010-09-08 05:09
You're right.
Added a new version of the patch.
msg116181 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-09-12 12:38
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.
msg116273 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2010-09-13 03:24
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
msg116274 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2010-09-13 03:26
I forgot to add:  The API that our (instrumented) malloc implementation is calling is:
PyGILState_GetThisThreadState();
msg116282 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-09-13 06:29
> You may find this hard to believe, but we do in fact embed python
> into other applications.

This is actually very easy to believe.

> 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.

This seems to be the core of the issue. Any other memory allocator
would *not* inquire about the state of Python. Any other memory
allocator would not even be aware that it is used by Python.

> What appMalloc is doing, in this case, is for every allocation, to
> get the python TLS pointer.  There is nothing wrong with this

I find this wrong. It violates the software layering. The memory
management layer is not supposed to access upper layers (such as
the interpreter state, or the application state).

> 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?

The code change itself is harmless, yes. The comment is not. It imposes
a requirement on Python (namely, that the malloc implementation may
be free to make calls to Python) which is harmful. The malloc
implementation just has no business looking at the thread state.

So I remain -1 on this change.
msg116287 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2010-09-13 07:53
>The malloc
>implementation just has no business looking at the thread state.

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.
msg116289 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-09-13 08:20
> Note that we are not making any new requirements on python here.

But you are. So far, there was no guarantee whatsoever about the state
of Python when malloc is called. You are now introducing a requirement
that Python must be in a certain state to make it correct to call
malloc. IOW, this innocent change actually introduces a new feature.

> So, although you have nothing against the patch as such

I think it's harmless - I don't think it is a good patch.
It shouldn't matter whether or not it is applied (i.e. there is no
change to Python that I can see).

> 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.

No. It's not the usage that I disapprove but, but the new requirement
on Python. If you were able to do your profiling in a manner compatible
with (the current) Python, it would be certainly fine with me.
msg156390 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2012-03-20 09:18
I'll rework this for python 3.x and see where that gets us.
msg156409 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2012-03-20 14:22
New patch, based on the cpython tip.
msg156411 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2012-03-20 14:25
hm, for some reason this patch isn't viewable in side-by-side
msg156483 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2012-03-21 09:17
Making this low priority since it applies only to platforms without Windows and pthread support.
msg157173 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2012-03-31 11:38
Closing this since it applies only to our custom tls implementation.  Most platforms use native tls now.
History
Date User Action Args
2012-03-31 11:38:29kristjan.jonssonsetstatus: open -> closed
resolution: wont fix
messages: + msg157173
2012-03-21 09:17:12kristjan.jonssonsetpriority: normal -> low

messages: + msg156483
versions: + Python 3.3, - Python 3.2
2012-03-20 14:25:41kristjan.jonssonsetmessages: + msg156411
2012-03-20 14:22:41kristjan.jonssonsetfiles: + tlspatch.patch

messages: + msg156409
2012-03-20 09:18:05kristjan.jonssonsetmessages: + msg156390
2010-09-13 08:21:00loewissetmessages: + msg116289
2010-09-13 07:53:05kristjan.jonssonsetmessages: + msg116287
2010-09-13 06:29:28loewissetmessages: + msg116282
2010-09-13 03:26:13kristjan.jonssonsetmessages: + msg116274
2010-09-13 03:24:27kristjan.jonssonsetmessages: + msg116273
2010-09-12 12:38:42loewissetkeywords: patch, patch
nosy: + loewis
messages: + msg116181

2010-09-08 05:09:43kristjan.jonssonsetkeywords: patch, patch
files: + tlspatch.patch
messages: + msg115848
2010-09-07 14:17:24pitrousetkeywords: patch, patch
nosy: + pitrou
messages: + msg115763

2010-09-07 06:03:04kristjan.jonssoncreate