Repository navigation
Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump
Description
| BPO | 4293 |
|---|---|
| Nosy | @amauryfa, @mdickinson, @pitrou, @kristjanvalur, @tiran |
| Files |
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:
assignee = None
closed_at = <Date 2009-01-20.12:02:07.568>
created_at = <Date 2008-11-10.11:36:34.958>
labels = ['interpreter-core', 'type-crash']
title = 'Thread Safe Py_AddPendingCall'
updated_at = <Date 2009-01-20.12:02:07.567>
user = 'https://github.com/kristjanvalur'bugs.python.org fields:
activity = <Date 2009-01-20.12:02:07.567>
actor = 'mark.dickinson'
assignee = 'none'
closed = True
closed_date = <Date 2009-01-20.12:02:07.568>
closer = 'mark.dickinson'
components = ['Interpreter Core']
creation = <Date 2008-11-10.11:36:34.958>
creator = 'kristjan.jonsson'
dependencies = []
files = ['11978', '12008', '12031', '12032', '12035', '12594', '12596', '12619', '12624']
hgrepos = []
issue_num = 4293
keywords = ['patch']
message_count = 32.0
messages = ['75691', '75692', '75693', '75700', '75782', '75783', '75872', '75963', '75966', '75970', '75977', '79113', '79119', '79128', '79132', '79142', '79147', '79151', '79152', '79241', '79255', '79269', '79270', '79290', '79504', '79507', '79511', '79658', '79975', '79982', '80091', '80250']
nosy_count = 5.0
nosy_names = ['amaury.forgeotdarc', 'mark.dickinson', 'pitrou', 'kristjan.jonsson', 'christian.heimes']
pr_nums = []
priority = 'normal'
resolution = 'accepted'
stage = 'patch review'
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue4293'
versions = ['Python 3.1', 'Python 2.7']Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump
Activity
kristjanvalur commentedon Nov 10, 2008
At CCP We have started using the Py_AddPendingCall() mechanism to
signal python about a completed IO operation.
However, we noticed that the existing mechanism was hoplelessly un-
thread safe. This is bad, since on Windows at least, it is very
convenient to have such callbacks happen on an arbitrary thread from
the system's thread pool.
I submit a thread-safe implementation instead to be used if WITH_THREAD
is defined.
This allows Py_AddPendingCall() to be called from any thread, from any
context, even from a PendingCall callback itself.
tiran commentedon Nov 10, 2008
Your patch sounds like a useful addition. However I can comment on the
details because I haven't dealt with Py_AddPendingCall() yet.
Documentation updates and a unit test (Modules/_testcapimodule.c), that
shows the bug and verifies your patch, are welcome, too.
kristjanvalur commentedon Nov 10, 2008
Good point. I'll make a test using ctypes and _testcapimodule.c to try
to make it as non-platform-specific as possible.
kristjanvalur commentedon Nov 10, 2008
Okay, i add a new patch that includes tests.
I am unsure where I should add documentation for the Py_AddPendingCall
() function. Any suggestions?
amauryfa commentedon Nov 12, 2008
Py_AddPendingCall is used inside signal handlers.
It seems that there is a (tiny) chance that a signal interrupts the
program inside Py_MakePendingCalls, while the pendinglock is held.
Py_AddPendingCall would try to acquire this lock again and deadlock.
kristjanvalur commentedon Nov 12, 2008
Right.
Isn't the best way to avoid it to block signals just while we pop the
queue? I'll see if python has a portable sigaction thing to do that.
Otherwise we'd have to start to mess with a "volatile busy" flag again
and possibly introduce tricky race conditions.
kristjanvalur commentedon Nov 14, 2008
Here is a revised version.
Since there is no portable way to block signals, and no way at all on
windows (apparently) the simplest way is to simply use NOWAIT_LOCK when
adding a new pending call. While this does not guarantee that we are
always able to append a call (we may be unlucky in all our N tries and
there is also no portable way to yield the thread timeslice between
tries) such are also the semantics of the method. Sometimes the buffer
may be full, or we fail to get the lock in time, and a -1 is returned.
It is up to the caller to respond appropriately, perhaps by trying again
later.
kristjanvalur commentedon Nov 17, 2008
Small refinement: Deadlock avoidance is only needed on the main thread
since we only install signal handlers for the main thread.
amauryfa commentedon Nov 17, 2008
Two comments about the patch:
typo in a comment: "...change chat a signal..."
PyThread_acquire_lock should not be called with a NULL lock: on win32
this does not matter (there is a test in thread_nt.h), but Unix
segfaults in thread_pthread.h if you hit Ctrl-C.
kristjanvalur commentedon Nov 17, 2008
I had forgotten that locks aren't initialized until PyEval_InitThreads
() is called. Now we check for this case. Also reinitialize when fork
() is called.
Is there any suggested place where I should add documentation to this?
kristjanvalur commentedon Nov 17, 2008
I turns out that for SIGINT, windows creates a new thread. So, the
assumption that signals are delivered on the main thread are not valid
in general. Therefore, we must initialize the lock pending_lock
independently of PyEval_InitThreads(), and also cannot assume that
signals are deliverd on the main thread.
kristjanvalur commentedon Jan 5, 2009
Ping.
Amaury, any thoughts?
amauryfa commentedon Jan 5, 2009
The things_to_do static variable should be declared as "volatile": it
is read by the main loop without any lock.
(by the way, could you rename it to something like "pendingcalls_to_do"?)
in the old Py_MakePendingCalls function, the "#ifdef WITH_THREAD" part
is not useful now.
the "XXX" comments should probably be rewritten.
A note to people concerned by performance: the additional lock is used
only when there is an actual pending call to process. The main loop only
regularly checks the "things_to_do static" volatile static variable, and
the patch does not change this.
8 remaining items
pitrou commentedon Jan 6, 2009
Ok, the test patch is fine!
Now looking at the main patch, some comments:
safe?
called? I know it's unlikely, but imagine someone calling that function
just after the interpreter has been initialized
Py_AddPendingCall is called, otherwise there can be a deadlock (is it a
change from previous versions by the way?)
a callback. There is no need for it to be, but if it aims to be, then it
should perhaps be documented and tested :)
pitrou commentedon Jan 6, 2009
Sorry, forget what I said about the GIL, it looks fine. Perhaps some
comments should be added to clarify that matter, however.
kristjanvalur commentedon Jan 6, 2009
the interpreter does (after executing one bytecode, I init _PyTicker to
0 now) is to initialize this lock. Creating a lock really must be done
by one well known startup thread only. There is a lot of functions in
the python API that don't work until after python initialization
(without that being explicitly documented), Py_AddPendingCall() is one
of them. Btw, there is no generic "init" function for the ceval.c
module. And PyEval_InitThreads isn't really suitable since we must
have the lock, even if we haven't initialized the threads, to guard us
against reentrancy during signal delivery. For extra safety, I have
added a NULL test in Py_AddPendingCall().
3)Py_AddPendingCall() can be called with our without the GIL.
4)I've added a check in Py_MakePendingCalls(). In PyEval_ReInitThreads
there is no way to do anything sensible in the case of error.
5)Yes it is reentrant. The pending_lock is only held for a short while
while we pop a callback off the queue. However, because even during
that short while a signal can be delivered which causes
Py_AddPendingCall() to be called, the latter function fails if it
doesn't acquire the lock in a reasonable amount of time. This would
cause a signal to be lost, and not a deadlock. This hasn't changed
from the old implementation.
I have also added a flag to make sure that we don't execute pending
calls recursively, (async notifications is so much better) and
explained it better in the docs.
kristjanvalur commentedon Jan 9, 2009
Checked in as revision: 68460
pitrou commentedon Jan 9, 2009
Looks like you forgot the unit tests!
kristjanvalur commentedon Jan 9, 2009
I indeed forgot the unittests and docs.
They are now in, in revision: 68461
kristjanvalur commentedon Jan 12, 2009
Added MISC/News entry in revision: 68545
mdickinson commentedon Jan 16, 2009
Kristján,
The r68461 checkin seems to be causing a number of the buildbots to fail,
typically with output like:
test_capi test test_capi failed -- Traceback (most recent call last): File "/home/pybot/buildarea/trunk.klose-debian-ppc/build/Lib/test/test_capi.py", line 57, in test_pendingcalls_threaded self.pendingcalls_wait(l, n) File "/home/pybot/buildarea/trunk.klose-debian-ppc/build/Lib/test/test_capi.py", line 42, in pendingcalls_wait "timeout waiting for %i callbacks, got %i"%(n, len(l))) AssertionError: timeout waiting for 32 callbacks, got 23Please could you look into this?
Also, I don't understand the code:
in pendingcalls_wait(), in test_capi.py. Whatever you're
trying to do here, surely there's a better way?
mdickinson commentedon Jan 16, 2009
How about using a timer instead of the 'count += 1' loop: after starting
the 32 self.pendingcalls_submit threads, set up a threading.Event and
start yet another thread that simply does a time.sleep(5.0) (or whatever)
and then sets that event.
Then your waiting loop could be something like:
There's probably a better way, but that's the best I can come up with this
late on a Friday night...
kristjanvalur commentedon Jan 18, 2009
checked in r68722, let's hope this fixes things.
mdickinson commentedon Jan 20, 2009
That seems to have done the trick. Thanks!