Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on shutdown #374

Merged
merged 1 commit into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/watchdog/observers/inotify_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def run(self):
IN_MOVE_TO event, remove the previous added matching IN_MOVE_FROM event
and add them back to the queue as a tuple.
"""
while self.should_keep_running():
deleted_self = False
while self.should_keep_running() and not deleted_self:
inotify_events = self._inotify.read_events()
for inotify_event in inotify_events:
logger.debug("in-event %s", inotify_event)
Expand All @@ -74,3 +75,7 @@ def matching_from_event(event):
else:
self._queue.put(inotify_event)

if inotify_event.is_delete_self and \
inotify_event.src_path == self._inotify.path:
# Deleted the watched directory, stop watching for events
deleted_self = True
5 changes: 3 additions & 2 deletions src/watchdog/observers/inotify_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ def close(self):
Closes the inotify instance and removes all associated watches.
"""
with self._lock:
wd = self._wd_for_path[self._path]
inotify_rm_watch(self._inotify_fd, wd)
if self._path in self._wd_for_path:
wd = self._wd_for_path[self._path]
inotify_rm_watch(self._inotify_fd, wd)
os.close(self._inotify_fd)

def read_events(self, event_buffer_size=DEFAULT_EVENT_BUFFER_SIZE):
Expand Down
15 changes: 14 additions & 1 deletion tests/test_inotify_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import random
import pytest
from tests import tmpdir, p # pytest magic
from .shell import mkdir, touch, mv
from .shell import mkdir, touch, mv, rm
from watchdog.observers.api import ObservedWatch
from watchdog.utils import platform

Expand Down Expand Up @@ -104,6 +104,19 @@ def test_move_internal_batch(p):
inotify.close()


@pytest.mark.timeout(5)
def test_delete_watched_directory(p):
mkdir(p('dir'))
inotify = InotifyBuffer(p('dir').encode())
rm(p('dir'), recursive=True)

# Wait for the event to be picked up
inotify.read_event()

# Ensure InotifyBuffer shuts down cleanly without raising an exception
inotify.close()


def test_close_should_terminate_thread(p):
inotify = InotifyBuffer(p('').encode(), recursive=True)
assert inotify.is_alive()
Expand Down