]> granicus.if.org Git - python/commitdiff
Merged revisions 78527,78531 via svnmerge from
authorGregory P. Smith <greg@mad-scientist.com>
Mon, 1 Mar 2010 04:40:36 +0000 (04:40 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Mon, 1 Mar 2010 04:40:36 +0000 (04:40 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78527 | gregory.p.smith | 2010-02-28 17:22:39 -0800 (Sun, 28 Feb 2010) | 4 lines

  Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
  thread could raise an incorrect RuntimeError about not holding the import
  lock.  The import lock is now reinitialized after fork.
........
  r78531 | gregory.p.smith | 2010-02-28 18:31:33 -0800 (Sun, 28 Feb 2010) | 2 lines

  Fix for r78527.  It left out updating forkpty.
........

Lib/test/test_thread.py
Misc/NEWS
Modules/posixmodule.c
Python/import.c

index 8bbfdb11edbfd6dc7160c7def16daa2aea4962f1..8dd8d1e897011ceaaf8767f3c391a2a59c30a584 100644 (file)
@@ -4,6 +4,7 @@ import random
 from test import test_support
 import thread
 import time
+import sys
 
 from test import lock_tests
 
@@ -169,8 +170,47 @@ class LockTests(lock_tests.LockTests):
     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 b05b8544611469c77984c51f9a2a63c272a9ba6e..f71e9bee0058d3fd4e54425335e54a4389f26e9c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -250,6 +250,10 @@ Extension Modules
 - Expat: Fix DoS via XML document with malformed UTF-8 sequences
   (CVE_2009_3560).
 
+- Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
+  thread could raise an incorrect RuntimeError about not holding the import
+  lock.  The import lock is now reinitialized after fork.
+
 Build
 -----
 
index 616722f55c56be92214df0663f1f41e149cc8be3..50ef80308edb3050e5198894b26e42bc1a0e95cb 100644 (file)
@@ -3632,14 +3632,18 @@ static PyObject *
 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 @@ static PyObject *
 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,
@@ -3777,16 +3785,20 @@ To both, return fd of newly opened pseudo-terminal.\n");
 static PyObject *
 posix_forkpty(PyObject *self, PyObject *noargs)
 {
-       int master_fd = -1, result;
+       int master_fd = -1, result = 0;
        pid_t pid;
 
        _PyImport_AcquireLock();
        pid = forkpty(&master_fd, NULL, NULL, NULL);
-       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,
index cbfb7615e2551356ccfdad4684f2cbb70dfe2cf8..43eabb830bdf4c8d540f2953651e438aae295cb7 100644 (file)
@@ -298,15 +298,17 @@ _PyImport_ReleaseLock(void)
 }
 
 /* This function is called from PyOS_AfterFork to ensure that newly
-   created child processes do not share locks with the parent. */
+   created child processes do not share locks with the parent.
+   We now acquire the import lock around fork() calls but on some platforms
+   (Solaris 9 and earlier? see isue7242) that still left us with problems. */
 
 void
 _PyImport_ReInitLock(void)
 {
-#ifdef _AIX
-       if (import_lock != NULL)
-               import_lock = PyThread_allocate_lock();
-#endif
+       if (import_lock != NULL)
+               import_lock = PyThread_allocate_lock();
+       import_lock_thread = -1;
+       import_lock_level = 0;
 }
 
 #endif