Skip to content

Commit 579b683

Browse files
jmswaneyjakirkham
authored andcommitted
#272 Fix race condition in DirectoryStore (#318)
* #272 fix race condition to make folders in directory store * #272 remove FileNotFoundError for python 2 support * Change OSError to KeyError to make tests pass * Only catch `OSError` if its `errno` equal `EEXIST` On Python 3, we would only want to catch the race case here by checking for the `FileExistsError` and suppressing raising for that case. However there is no `FileExistsError` on Python 2. Only `OSError` exists on Python 2/3. However that catches some other errors that we might not want to suppress here. So we check the `errno` of the `OSError` instance to verify it is caused by an existing file/directory represented by the `EEXIST` error number. This amounts to catching the `FileExistsError` in a Python 2/3 friendly way. All other `OSErrors` we raise as before. * Drop generic `Exception` case in `DirectoryStore` As we now properly handle the existing file case properly, go ahead and drop the generic exception case. This should now raise some errors that were hidden previously.
1 parent f582737 commit 579b683

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

zarr/storage.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import zipfile
2424
import shutil
2525
import atexit
26+
import errno
2627
import re
2728
import sys
2829
import json
@@ -745,8 +746,9 @@ def __setitem__(self, key, value):
745746
if not os.path.exists(dir_path):
746747
try:
747748
os.makedirs(dir_path)
748-
except Exception:
749-
raise KeyError(key)
749+
except OSError as e:
750+
if e.errno != errno.EEXIST:
751+
raise KeyError(key)
750752

751753
# write to temporary file
752754
temp_path = None

zarr/tests/data/store.zip

107 Bytes
Binary file not shown.

zarr/tests/data/store/foo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bar

0 commit comments

Comments
 (0)