Skip to content

Commit

Permalink
Avoid race condition crash on directory deletion
Browse files Browse the repository at this point in the history
Given the directory structure /a/b/c.txt there is a race condition
in DirectorySnapshot if it reads the list of entries in /a, then
/a/b is deleted, and then it tries to read /a/b. This happens
often in practice when changing between very different branches in
git.

The correct behaviour would be to report /a/b as not existing in this
case, but we cannot do this without either changing the order of the
walk (from parent-first to parent-last) or significantly increasing
memory usage and copies, so instead we report /a/b as existing but
empty, which is not ideal, but is better than the current behavior
of silently crashing.
  • Loading branch information
Theo Spears committed Oct 23, 2014
1 parent f34d475 commit b20604a
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/watchdog/utils/dirsnapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,16 @@ def __init__(self, path, recursive=True,
self._inode_to_path[(st.st_ino, st.st_dev)] = path

def walk(root):
paths = [os.path.join(root, name) for name in listdir(root)]
try:
paths = [os.path.join(root, name) for name in listdir(root)]
except OSError as e:
# Directory may have been deleted between finding it in the directory
# list of its parent and trying to delete its contents. If this
# happens we treat it as empty, which is wrong but better than crashing
if e.errno == 2:
return
else:
raise
entries = []
for p in paths:
try:
Expand Down

0 comments on commit b20604a

Please sign in to comment.