[New-bugs-announce] [issue9573] imporing a module that executes fork() raises RuntimeError

Alex Roitman report at bugs.python.org
Thu Aug 12 01:42:04 CEST 2010


New submission from Alex Roitman <rshura at gmail.com>:

Importing the module with the following contents results in RuntimeError:

==================
import os
pid = os.fork()

if pid == 0:
    print "In the child"
else:
    print "In the parent"
print "Done\n"
==================

Running the same module as main works just fine, so it seems to be a purely import issue.

I looked into the 2.6.6rc1 source.  This is what I think happens in Python/import.c file:

1. After the fork() call, _PyImport_ReInitLock() is run. It sets import_lock_thread to -1 in the child, line 310.

2. In _PyImport_ReleaseLock() line 290 compares import_loc_thread to the current thread id, and if they are not the same then -1 is returned, which results in RuntimeError in PyImport_ImportModuleLevel (line 2186-2189)

So this is guaranteed to happen in the child, every time fork() is executed inside the module being imported.  If I change line 290 to be:

    if (import_lock_thread != me && import_lock_thread != -1)

then import proceeds fine, although I'm not sure this is a proper solution.

This happens on Linux, Darwin, and Cygwin, with python 2.6.5 and higher. I'd be happy to assist solving this, please let me know how I can help.

----------
components: Interpreter Core
messages: 113643
nosy: Alex.Roitman
priority: normal
severity: normal
status: open
title: imporing a module that executes fork() raises RuntimeError
type: behavior
versions: Python 2.6, Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9573>
_______________________________________


More information about the New-bugs-announce mailing list