Fix a concurrency issue happening with deep dependencies when the intermediary
directory doesn't exist, on fresh checkout and --jobs >1.
It happens in the case where there is 3 dependencies:
a
a/b/c/d
a/b/c/e
'a' is processed first. Then 'a/b/c/d' and 'a/b/c/e' are fired simultaneously.
If both are not present, both svn checkout tries to mkdir b and b/c and a race
condition occurs between their call for isdir() and mkdir().
This works around the issue by creating the intermediary directories ourself
before calling out svn and managing the error ourself.
TBR=dpranke@chromium.org
BUG=
TEST=fresh checkout shouldn't be flaky
Review URL: http://codereview.chromium.org/8359018
git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@106636 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/gclient_utils.py b/gclient_utils.py
index 918b489..e658934 100644
--- a/gclient_utils.py
+++ b/gclient_utils.py
@@ -171,6 +171,26 @@
RemoveDirectory = rmtree
+def safe_makedirs(tree):
+ """Creates the directory in a safe manner.
+
+ Because multiple threads can create these directories concurently, trap the
+ exception and pass on.
+ """
+ count = 0
+ while not os.path.exists(tree):
+ count += 1
+ try:
+ os.makedirs(tree)
+ except OSError, e:
+ # 17 POSIX, 183 Windows
+ if e.errno not in (17, 183):
+ raise
+ if count > 40:
+ # Give up.
+ raise
+
+
def CheckCallAndFilterAndHeader(args, always=False, **kwargs):
"""Adds 'header' support to CheckCallAndFilter.