This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Alex.Roitman
Recipients Alex.Roitman
Date 2010-08-11.23:42:01
SpamBayes Score 1.7788421e-06
Marked as misclassified No
Message-id <1281570124.9.0.596338600617.issue9573@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2010-08-11 23:42:05Alex.Roitmansetrecipients: + Alex.Roitman
2010-08-11 23:42:04Alex.Roitmansetmessageid: <1281570124.9.0.596338600617.issue9573@psf.upfronthosting.co.za>
2010-08-11 23:42:02Alex.Roitmanlinkissue9573 messages
2010-08-11 23:42:01Alex.Roitmancreate