diff options
author | Armin Rigo <arigo@tunes.org> | 2013-04-29 11:24:57 +0200 |
---|---|---|
committer | Armin Rigo <arigo@tunes.org> | 2013-04-29 11:24:57 +0200 |
commit | e89254d38a4fb45d150e4b444ac418afb7980e1f (patch) | |
tree | 94b7d389648174c8eb422cf83b17030697a2f5a3 /py | |
parent | Be more multiprocess-safe. (diff) | |
download | pypy-e89254d38a4fb45d150e4b444ac418afb7980e1f.tar.gz pypy-e89254d38a4fb45d150e4b444ac418afb7980e1f.tar.bz2 pypy-e89254d38a4fb45d150e4b444ac418afb7980e1f.zip |
Increase the resistence of make_numbered_dir() against multiple
processes
Diffstat (limited to 'py')
-rw-r--r-- | py/_path/local.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/py/_path/local.py b/py/_path/local.py index 3c8c0c503a..cd49bd5433 100644 --- a/py/_path/local.py +++ b/py/_path/local.py @@ -655,7 +655,8 @@ class LocalPath(FSBase): mkdtemp = classmethod(mkdtemp) def make_numbered_dir(cls, prefix='session-', rootdir=None, keep=3, - lock_timeout = 172800): # two days + lock_timeout = 172800, # two days + min_timeout = 300): # five minutes """ return unique directory with a number greater than the current maximum one. The number is assumed to start directly after prefix. if keep is true directories with a number less than (maxnum-keep) @@ -723,6 +724,20 @@ class LocalPath(FSBase): for path in rootdir.listdir(): num = parse_num(path) if num is not None and num <= (maxnum - keep): + if min_timeout: + # NB: doing this is needed to prevent (or reduce + # a lot the chance of) the following situation: + # 'keep+1' processes call make_numbered_dir() at + # the same time, they create dirs, but then the + # last process notices the first dir doesn't have + # (yet) a .lock in it and kills it. + try: + t1 = path.lstat().mtime + t2 = lockfile.lstat().mtime + if abs(t2-t1) < min_timeout: + continue # skip directories too recent + except py.error.Error: + continue # failure to get a time, better skip lf = path.join('.lock') try: t1 = lf.lstat().mtime |