From 0e42f0e7991e0ae7484ea7cdc339e6293a5fd169 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 19 Jan 2008 22:35:09 +0000 Subject: Backport r60104 + r60111 from trunk. - Issue #1336: fix a race condition in subprocess.Popen if the garbage collector kicked in at the wrong time that would cause the process to hang when the child wrote to stderr. --- Lib/subprocess.py | 14 +++++++++++++- Misc/NEWS | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index e1b0c1f..f831083 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -346,6 +346,7 @@ mswindows = (sys.platform == "win32") import os import types import traceback +import gc if mswindows: import threading @@ -899,7 +900,16 @@ class Popen(object): errpipe_read, errpipe_write = os.pipe() self._set_cloexec_flag(errpipe_write) - self.pid = os.fork() + gc_was_enabled = gc.isenabled() + # Disable gc to avoid bug where gc -> file_dealloc -> + # write to stderr -> hang. http://bugs.python.org/issue1336 + gc.disable() + try: + self.pid = os.fork() + except: + if gc_was_enabled: + gc.enable() + raise if self.pid == 0: # Child try: @@ -958,6 +968,8 @@ class Popen(object): os._exit(255) # Parent + if gc_was_enabled: + gc.enable() os.close(errpipe_write) if p2cread and p2cwrite: os.close(p2cread) diff --git a/Misc/NEWS b/Misc/NEWS index 002ea18..5f674fe 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -26,6 +26,10 @@ Extension Modules Library ------- +- Issue #1336: fix a race condition in subprocess.Popen if the garbage + collector kicked in at the wrong time that would cause the process + to hang when the child wrote to stderr. + - Bug #1728403: Fix a bug that CJKCodecs StreamReader hangs when it reads a file that ends with incomplete sequence and sizehint argument for .read() is specified. -- cgit v0.12