Re: [Python-Dev] pthreads, fork, import, and execvp

2009-09-16 Thread Thomas Wouters
On Wed, Sep 9, 2009 at 23:56, Nick Coghlan ncogh...@gmail.com wrote: Thomas Wouters wrote: Your idea of making this an API called a 'fork lock' or something sounds good (though I think it needs a better name. PyBeforeFork PyAfterFork?). The subprocess module, for example,

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-09-11 Thread Cameron Simpson
On 25Jul2009 10:25, Gregory P. Smith g...@krypto.org wrote: | On Thu, Jul 23, 2009 at 4:28 PM, Thomas Wouterstho...@python.org wrote: | So attached (and at http://codereview.appspot.com/96125/show ) is a | preliminary fix, correcting the problem with os.fork(), os.forkpty() and | os.fork1().

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-09-11 Thread Thomas Wouters
On Fri, Sep 11, 2009 at 08:06, Cameron Simpson c...@zip.com.au wrote: On 25Jul2009 10:25, Gregory P. Smith g...@krypto.org wrote: | On Thu, Jul 23, 2009 at 4:28 PM, Thomas Wouterstho...@python.org wrote: | So attached (and at http://codereview.appspot.com/96125/show ) is a | preliminary

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-09-09 Thread Thomas Wouters
On Sat, Jul 25, 2009 at 19:25, Gregory P. Smith g...@krypto.org wrote: On Thu, Jul 23, 2009 at 4:28 PM, Thomas Wouterstho...@python.org wrote: So attached (and at http://codereview.appspot.com/96125/show ) is a preliminary fix, correcting the problem with os.fork(), os.forkpty() and

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-09-09 Thread Gregory P. Smith
On Wed, Sep 9, 2009 at 9:07 AM, Thomas Wouterstho...@python.org wrote: On Sat, Jul 25, 2009 at 19:25, Gregory P. Smith g...@krypto.org wrote: On Thu, Jul 23, 2009 at 4:28 PM, Thomas Wouterstho...@python.org wrote: So attached (and at http://codereview.appspot.com/96125/show ) is a

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-09-09 Thread Gregory P. Smith
On Wed, Sep 9, 2009 at 11:36 AM, Thomas Wouterstho...@python.org wrote: On Wed, Sep 9, 2009 at 20:19, Gregory P. Smith g...@krypto.org wrote: Take a look at http://code.google.com/p/python-atfork/ which I created to address the general fork+threading with locks held causing deadlocks issue

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-09-09 Thread Thomas Wouters
On Wed, Sep 9, 2009 at 20:19, Gregory P. Smith g...@krypto.org wrote: On Wed, Sep 9, 2009 at 9:07 AM, Thomas Wouterstho...@python.org wrote: On Sat, Jul 25, 2009 at 19:25, Gregory P. Smith g...@krypto.org wrote: On Thu, Jul 23, 2009 at 4:28 PM, Thomas Wouterstho...@python.org wrote:

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-09-09 Thread Nick Coghlan
Thomas Wouters wrote: Your idea of making this an API called a 'fork lock' or something sounds good (though I think it needs a better name. PyBeforeFork PyAfterFork?). The subprocess module, for example, disables garbage collection before forking and restores it afterwards

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-07-25 Thread Gregory P. Smith
On Thu, Jul 23, 2009 at 4:28 PM, Thomas Wouterstho...@python.org wrote: So attached (and at http://codereview.appspot.com/96125/show ) is a preliminary fix, correcting the problem with os.fork(), os.forkpty() and os.fork1(). This doesn't expose a general API for C code to use, for two

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-07-23 Thread Thomas Wouters
So attached (and at http://codereview.appspot.com/96125/show ) is a preliminary fix, correcting the problem with os.fork(), os.forkpty() and os.fork1(). This doesn't expose a general API for C code to use, for two reasons: it's not easy, and I need this fix more than I need the API change :-) (I

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-07-20 Thread Mike Klaas
On Thu, Jul 16, 2009 at 1:08 PM, Thomas Wouters tho...@python.org wrote: Picking up a rather old discussion... We encountered this bug at Google and I'm now incentivized to fix it. For a short recap: Python has an import lock that prevents more than one thread from doing an import at any

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-07-20 Thread Thomas Wouters
On Mon, Jul 20, 2009 at 11:26, Mike Klaas mike.kl...@gmail.com wrote: On Thu, Jul 16, 2009 at 1:08 PM, Thomas Wouters tho...@python.org wrote: Picking up a rather old discussion... We encountered this bug at Google and I'm now incentivized to fix it. For a short recap: Python has an

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-07-16 Thread Thomas Wouters
On Thu, May 18, 2006 at 20:02, Martin v. Löwis mar...@v.loewis.de wrote: Nick Coghlan wrote: And if I understand it correctly, it falls under the category that waiting for another thread while holding the import lock is a *really* bad idea from a thread safety point of view. The thing

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-07-16 Thread Nick Coghlan
Thomas Wouters wrote: Picking up a rather old discussion... We encountered this bug at Google and I'm now incentivized to fix it. For a short recap: Python has an import lock that prevents more than one thread from doing an import at any given time. However, unlike most of the locks we have

Re: [Python-Dev] pthreads, fork, import, and execvp

2009-07-16 Thread Lisandro Dalcin
On Thu, May 18, 2006 at 3:02 PM, Martin v. Löwismar...@v.loewis.de wrote: Right. With some googling, I found that one solution is pthread_atexit: a pthread_atexit handler is a triple (before, in_parent, in_child). You set it to (acquire, release, release). Did you mean pthread_atfork() ?

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-18 Thread Neal Norwitz
On 5/16/06, Martin v. Löwis [EMAIL PROTECTED] wrote: Nick Coghlan wrote: Broadening the ifdef to cover all posix systems rather than just AIX might be the right thing to do. As I said: I doubt it is the right thing to do. Instead, the lock should get released in the child process if it is

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-18 Thread Martin v. Löwis
Neal Norwitz wrote: As I said: I doubt it is the right thing to do. Instead, the lock should get released in the child process if it is held at the point of the fork. I'm not sure if this bug can be reproduced now, but the comment in warnings.py points to: http://python.org/sf/683658

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-18 Thread Nick Coghlan
Martin v. Löwis wrote: Neal Norwitz wrote: As I said: I doubt it is the right thing to do. Instead, the lock should get released in the child process if it is held at the point of the fork. I'm not sure if this bug can be reproduced now, but the comment in warnings.py points to:

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-18 Thread Ronald Oussoren
On 16-mei-2006, at 23:20, Martin v. Löwis wrote: Nick Coghlan wrote: Broadening the ifdef to cover all posix systems rather than just AIX might be the right thing to do. As I said: I doubt it is the right thing to do. Instead, the lock should get released in the child process if it is held

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-18 Thread Martin v. Löwis
Nick Coghlan wrote: And if I understand it correctly, it falls under the category that waiting for another thread while holding the import lock is a *really* bad idea from a thread safety point of view. The thing with the import-after-fork deadlock is that you can trigger it without even

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-18 Thread Martin v. Löwis
Ronald Oussoren wrote: Wouldn't this specific problem be fixed if os.fork were to acquire the import lock before calling fork(2)? Right. Of course, you need to release the lock then both in the parent and the child. Also, all places that currently do PyOs_AfterFork should get modified (i.e.

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-17 Thread Yair Chuchem
On 5/16/06, Martin v. Löwis [EMAIL PROTECTED] wrote: Rotem should simply avoid to fork() in the toplevel code of a module.this is not what happened.we called subprocess which itself called fork.as a subprocess bug this could be easily fixed by moving the import in os._execve. we have made that

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-16 Thread Martin v. Löwis
Nick Coghlan wrote: Rotem took the time to explain the problem to me more fully in private email, and it appears that using import statements inside functions is currently fundamentally unsafe in a posix environment where both multiple threads and fork() are used. If any thread other than

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-16 Thread Nick Coghlan
Martin v. Löwis wrote: So if the the import lock was held during the fork call, the new thread will hold the import lock of the new process, and subsequent imports will block. However, then the problem is not with the execve implementation, but with the fact that the import lock was held

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-16 Thread Martin v. Löwis
Rotem Yaari wrote: Rotem should simply avoid to fork() in the toplevel code of a module. That's exactly the problem - the fork is not done at the module level, and the reason for the deadlock is imports inside functions performed by various library functions. This can happen virtually in

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-15 Thread Nick Coghlan
Guido van Rossum wrote: Yeah, I think imports inside functions are overused. Rotem took the time to explain the problem to me more fully in private email, and it appears that using import statements inside functions is currently fundamentally unsafe in a posix environment where both multiple

Re: [Python-Dev] pthreads, fork, import, and execvp

2006-05-12 Thread Nick Coghlan
Rotem Yaari wrote: This has occurred on Python 2.4.1 on a 2.4.27 Linux kernel. Preliminary analysis of the hang shows that the child process blocks upon entering the execvp function, in which the import_lock is acquired due to the following line: def _ execvpe(file, args, env=None):

[Python-Dev] pthreads, fork, import, and execvp

2006-05-11 Thread Rotem Yaari
Hello everyone! We have been encountering several deadlocks in a threaded Python application which calls subprocess.Popen (i.e. fork()) in some of its threads. This has occurred on Python 2.4.1 on a 2.4.27 Linux kernel. Preliminary analysis of the hang shows that the child process blocks upon