Index: Python/import.c =================================================================== --- Python/import.c (revision 78514) +++ Python/import.c (working copy) @@ -303,10 +303,10 @@ void _PyImport_ReInitLock(void) { -#ifdef _AIX if (import_lock != NULL) import_lock = PyThread_allocate_lock(); -#endif + import_lock_thread = -1; + import_lock_level = 0; } #endif Index: Lib/test/test_thread.py =================================================================== --- Lib/test/test_thread.py (revision 78514) +++ Lib/test/test_thread.py (working copy) @@ -4,6 +4,7 @@ from test import test_support import thread import time +import sys from test import lock_tests @@ -169,8 +170,47 @@ locktype = thread.allocate_lock +class TestForkInThread(unittest.TestCase): + def setUp(self): + self.read_fd, self.write_fd = os.pipe() + + def test_forkinthread(self): + if sys.platform.startswith('win'): + from test.test_support import TestSkipped + raise TestSkipped("This test is only appropriate for " + "POSIX-like systems.") + def thread1(): + try: + pid = os.fork() # fork in a thread + except RuntimeError: + sys.exit(0) # exit the child + + if pid == 0: # child + os.close(self.read_fd) + os.write(self.write_fd, "OK") + sys.exit(0) + else: # parent + os.close(self.write_fd) + + thread.start_new_thread(thread1, ()) + self.assertEqual(os.read(self.read_fd, 2), "OK", + "Unable to fork() in thread") + + def tearDown(self): + try: + os.close(self.read_fd) + except OSError: + pass + + try: + os.close(self.write_fd) + except OSError: + pass + + def test_main(): - test_support.run_unittest(ThreadRunningTests, BarrierTest, LockTests) + test_support.run_unittest(ThreadRunningTests, BarrierTest, LockTests, + TestForkInThread) if __name__ == "__main__": test_main() Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (revision 78514) +++ Modules/posixmodule.c (working copy) @@ -3632,14 +3632,18 @@ posix_fork1(PyObject *self, PyObject *noargs) { pid_t pid; - int result; + int result = 0; _PyImport_AcquireLock(); pid = fork1(); - result = _PyImport_ReleaseLock(); + if (pid == 0) { + /* child: this clobbers and resets the import lock. */ + PyOS_AfterFork(); + } else { + /* parent: release the import lock. */ + result = _PyImport_ReleaseLock(); + } if (pid == -1) return posix_error(); - if (pid == 0) - PyOS_AfterFork(); if (result < 0) { /* Don't clobber the OSError if the fork failed. */ PyErr_SetString(PyExc_RuntimeError, @@ -3661,14 +3665,18 @@ posix_fork(PyObject *self, PyObject *noargs) { pid_t pid; - int result; + int result = 0; _PyImport_AcquireLock(); pid = fork(); - result = _PyImport_ReleaseLock(); + if (pid == 0) { + /* child: this clobbers and resets the import lock. */ + PyOS_AfterFork(); + } else { + /* parent: release the import lock. */ + result = _PyImport_ReleaseLock(); + } if (pid == -1) return posix_error(); - if (pid == 0) - PyOS_AfterFork(); if (result < 0) { /* Don't clobber the OSError if the fork failed. */ PyErr_SetString(PyExc_RuntimeError, @@ -3782,11 +3790,12 @@ _PyImport_AcquireLock(); pid = forkpty(&master_fd, NULL, NULL, NULL); + if (pid == 0) + PyOS_AfterFork(); + result = _PyImport_ReleaseLock(); if (pid == -1) return posix_error(); - if (pid == 0) - PyOS_AfterFork(); if (result < 0) { /* Don't clobber the OSError if the fork failed. */ PyErr_SetString(PyExc_RuntimeError,