Skip to content

Commit

Permalink
Backport PR ipython#4748: fix race condition in profiledir creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk authored and pankajp committed Feb 18, 2014
1 parent 182ceb3 commit 63d7f95
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions IPython/core/profiledir.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import os
import shutil
import errno
import time

from IPython.config.configurable import LoggingConfigurable
from IPython.utils.path import get_ipython_package_dir, expand_path
Expand Down Expand Up @@ -76,9 +77,17 @@ def _location_changed(self, name, old, new):
if self._location_isset:
raise RuntimeError("Cannot set profile location more than once.")
self._location_isset = True
if not os.path.isdir(new):
os.makedirs(new)

num_tries = 0
max_tries = 5
while not os.path.isdir(new):
try:
os.makedirs(new)
except OSError:
if num_tries > max_tries:
raise
num_tries += 1
time.sleep(0.5)

# ensure config files exist:
self.security_dir = os.path.join(new, self.security_dir_name)
self.log_dir = os.path.join(new, self.log_dir_name)
Expand All @@ -88,12 +97,12 @@ def _location_changed(self, name, old, new):

def _log_dir_changed(self, name, old, new):
self.check_log_dir()

def _mkdir(self, path, mode=None):
"""ensure a directory exists at a given path
This is a version of os.mkdir, with the following differences:
- returns True if it created the directory, False otherwise
- ignores EEXIST, protecting against race conditions where
the dir may have been created in between the check and
Expand All @@ -120,7 +129,7 @@ def _mkdir(self, path, mode=None):
return False
else:
raise

return True

def check_log_dir(self):
Expand Down Expand Up @@ -248,5 +257,3 @@ def find_profile_dir(cls, profile_dir, config=None):
if not os.path.isdir(profile_dir):
raise ProfileDirError('Profile directory not found: %s' % profile_dir)
return cls(location=profile_dir, config=config)


0 comments on commit 63d7f95

Please sign in to comment.