[python] fix deadlock issue

tomspur tomspur at fedoraproject.org
Sat Feb 25 20:35:20 UTC 2012


commit 9bf1f8f3bb1f47074db70e957142c99a91c3e673
Author: Thomas Spura <thomas.spura at gmail.com>
Date:   Sat Feb 25 21:34:30 2012 +0100

    fix deadlock issue

 python-3.2.2-fork-deadlock.patch |   62 ++++++++++++++++++++++++++++++++++++++
 python.spec                      |   11 ++++++-
 2 files changed, 72 insertions(+), 1 deletions(-)
---
diff --git a/python-3.2.2-fork-deadlock.patch b/python-3.2.2-fork-deadlock.patch
new file mode 100644
index 0000000..6b1bcff
--- /dev/null
+++ b/python-3.2.2-fork-deadlock.patch
@@ -0,0 +1,62 @@
+
+# HG changeset patch
+# User Charles-François Natali <neologix at free.fr>
+# Date 1328209039 -3600
+# Node ID c3649173d093ce3bb2f887c1b4c3207196f1f453
+# Parent  0b8917fc6db55d371573398c2ae29b120be40a19
+Issue #13817: After fork(), reinit the ad-hoc TLS implementation earlier to fix
+a random deadlock when fork() is called in a multithreaded process in debug
+mode, and make PyOS_AfterFork() more robust.
+
+diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
+--- a/Lib/test/test_threading.py
++++ b/Lib/test/test_threading.py
+@@ -635,6 +635,29 @@ class ThreadJoinOnShutdown(BaseTestCase)
+         output = "end of worker thread\nend of main thread\n"
+         self.assertScriptHasOutput(script, output)
+ 
++    @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
++    def test_reinit_tls_after_fork(self):
++        # Issue #13817: fork() would deadlock in a multithreaded program with
++        # the ad-hoc TLS implementation.
++
++        def do_fork_and_wait():
++            # just fork a child process and wait it
++            pid = os.fork()
++            if pid > 0:
++                os.waitpid(pid, 0)
++            else:
++                os._exit(0)
++
++        # start a bunch of threads that will fork() child processes
++        threads = []
++        for i in range(16):
++            t = threading.Thread(target=do_fork_and_wait)
++            threads.append(t)
++            t.start()
++
++        for t in threads:
++            t.join()
++
+ 
+ class ThreadingExceptionTests(BaseTestCase):
+     # A RuntimeError should be raised if Thread.start() is called
+diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
+--- a/Modules/signalmodule.c
++++ b/Modules/signalmodule.c
+@@ -976,11 +976,13 @@ void
+ PyOS_AfterFork(void)
+ {
+ #ifdef WITH_THREAD
++    /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
++     * can be called safely. */
++    PyThread_ReInitTLS();
+     _PyGILState_Reinit();
+     PyEval_ReInitThreads();
+     main_thread = PyThread_get_thread_ident();
+     main_pid = getpid();
+     _PyImport_ReInitLock();
+-    PyThread_ReInitTLS();
+ #endif
+ }
+
diff --git a/python.spec b/python.spec
index d82a615..b39ebd1 100644
--- a/python.spec
+++ b/python.spec
@@ -108,7 +108,7 @@ Summary: An interpreted, interactive, object-oriented programming language
 Name: %{python}
 # Remember to also rebase python-docs when changing this:
 Version: 2.7.2
-Release: 19%{?dist}
+Release: 20%{?dist}
 License: Python
 Group: Development/Languages
 Requires: %{python}-libs%{?_isa} = %{version}-%{release}
@@ -601,6 +601,11 @@ Patch147: 00147-add-debug-malloc-stats.patch
 # Taken from upstream http://bugs.python.org/issue13007 (rhbz#742242)
 Patch148: 00148-gdbm-1.9-magic-values.patch
 
+# Fix deadlock in fork:
+# https://bugzilla.redhat.com/show_bug.cgi?id=787712
+# http://bugs.python.org/issue13817
+Patch151: python-3.2.2-fork-deadlock.patch
+
 # python3.spec's
 #   Patch149: 00149-backport-issue11254-pycache-bytecompilation-fix.patch
 # is not relevant for Python 2
@@ -913,6 +918,7 @@ done
 %patch147 -p1
 %patch148 -p1
 # 00149: not for python 2
+%patch151 -p1
 
 
 # This shouldn't be necesarry, but is right now (2.2a3)
@@ -1741,6 +1747,9 @@ rm -fr %{buildroot}
 # ======================================================
 
 %changelog
+* Sat Feb 25 2012 Thomas Spura <tomspur at fedoraproject.org> - 2.7.2-20
+- fix deadlock issue (#787712)
+
 * Fri Feb 17 2012 Toshio Kuratomi <toshio at fedoraproject.org> - 2.7.2-19
 - Obsolete python-sqlite2
 


More information about the scm-commits mailing list