Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1542)

Unified Diff: subprocess2.py

Issue 1343783004: Work around race condition in subprocess. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: subprocess2.py
diff --git a/subprocess2.py b/subprocess2.py
index 21e34878516b20ca7b260420d9a526cd0eb40b4a..a00592f4774a83eaa8e697d38c8ab7a921903fa9 100644
--- a/subprocess2.py
+++ b/subprocess2.py
@@ -181,6 +181,14 @@ class Popen(subprocess.Popen):
Note: Popen() can throw OSError when cwd or args[0] doesn't exist. Translate
exceptions generated by cygwin when it fails trying to emulate fork().
"""
+ # subprocess.Popen.__init__() is not threadsafe; there is a race between
+ # creating the exec-error pipe for the child and setting it to CLOEXEC during
+ # which another thread can fork and cause the pipe to be inherited by its
+ # descendents, which will cause the current Popen to hang until all those
+ # descendents exit. Protect this with a lock so that only one fork/exec can
+ # happen at a time.
+ popen_lock = threading.Lock()
+
def __init__(self, args, **kwargs):
# Make sure we hack subprocess if necessary.
hack_subprocess()
@@ -234,7 +242,8 @@ class Popen(subprocess.Popen):
self.returncode = None
try:
- super(Popen, self).__init__(args, **kwargs)
+ with self.popen_lock:
+ super(Popen, self).__init__(args, **kwargs)
except OSError, e:
if e.errno == errno.EAGAIN and sys.platform == 'cygwin':
# Convert fork() emulation failure into a CygwinRebaseError().
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698