Skip to content

Commit

Permalink
[inotify] Allow to stop the emitter multiple times (#760)
Browse files Browse the repository at this point in the history
Fixes #758.

* set _inotify to None after stopping the emitter

* adapt test teardown

* catch OSError when closing watched file descriptor

* check event.is_directory when root is deleted

* Add a NEWs entry [skip-ci]

Co-authored-by: Mickaël Schoentgen <contact@tiger-222.fr>
  • Loading branch information
SamSchott and BoboTiG committed Feb 8, 2021
1 parent 72a2545 commit 16d9010
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Changelog

- Avoid deprecated ``PyEval_InitThreads`` on Python 3.7+ (`#746 <https://github.com/gorakhargosh/watchdog/pull/746>`_)
- [inotify] Add support for ``IN_CLOSE_WRITE`` events. A ``FileCloseEvent`` event will be fired. Note that ``IN_CLOSE_NOWRITE`` events are not handled to prevent much noise. (`#184 <https://github.com/gorakhargosh/watchdog/pull/184>`_, `#245 <https://github.com/gorakhargosh/watchdog/pull/245>`_, `#280 <https://github.com/gorakhargosh/watchdog/pull/280>`_, `#313 <https://github.com/gorakhargosh/watchdog/pull/313>`_, `#690 <https://github.com/gorakhargosh/watchdog/pull/690>`_)
- [inotify] Allow to stop the emitter multiple times (`#760 <https://github.com/gorakhargosh/watchdog/pull/760>`_)
- [mac] Support coalesced filesystem events (`#734 <https://github.com/gorakhargosh/watchdog/pull/734>`_)
- [mac] Drop support for OSX 10.12 and earlier (`#750 <https://github.com/gorakhargosh/watchdog/pull/750>`_)
- [mac] Fix an issue when renaming an item changes only the casing (`#750 <https://github.com/gorakhargosh/watchdog/pull/750>`_)
Expand Down
4 changes: 3 additions & 1 deletion src/watchdog/observers/inotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def on_thread_start(self):
def on_thread_stop(self):
if self._inotify:
self._inotify.close()
self._inotify = None

def queue_events(self, timeout, full_events=False):
# If "full_events" is true, then the method will report unmatched move events as separate events
Expand Down Expand Up @@ -179,7 +180,8 @@ def queue_events(self, timeout, full_events=False):
# cls = FileClosedEvent
# self.queue_event(cls(src_path))
elif event.is_delete_self and src_path == self.watch.path:
self.queue_event(DirDeletedEvent(src_path))
cls = DirDeletedEvent if event.is_directory else FileDeletedEvent
self.queue_event(cls(src_path))
self.stop()

def _decode_path(self, path):
Expand Down
7 changes: 6 additions & 1 deletion src/watchdog/observers/inotify_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,12 @@ def close(self):
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)

try:
os.close(self._inotify_fd)
except OSError:
# descriptor may be invalid because file was deleted
pass

def read_events(self, event_buffer_size=DEFAULT_EVENT_BUFFER_SIZE):
"""
Expand Down
6 changes: 1 addition & 5 deletions tests/test_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ def setup_teardown(tmpdir):

yield

try:
emitter.stop()
except OSError:
# watch was already stopped, e.g., in `test_delete_self`
pass
emitter.stop()
emitter.join(5)
assert not emitter.is_alive()

Expand Down
6 changes: 1 addition & 5 deletions tests/test_inotify_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ def watching(path=None, use_full_emitter=False):
emitter = Emitter(event_queue, ObservedWatch(path, recursive=True))
emitter.start()
yield
try:
emitter.stop()
except OSError:
# watch was already stopped, e.g., because root was deleted
pass
emitter.stop()
emitter.join(5)


Expand Down

0 comments on commit 16d9010

Please sign in to comment.