Skip to content

Commit 16d9010

Browse files
SamSchottBoboTiG
andauthored
[inotify] Allow to stop the emitter multiple times (#760)
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 <[email protected]>
1 parent 72a2545 commit 16d9010

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Changelog
1010

1111
- Avoid deprecated ``PyEval_InitThreads`` on Python 3.7+ (`#746 <https://github.com/gorakhargosh/watchdog/pull/746>`_)
1212
- [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>`_)
13+
- [inotify] Allow to stop the emitter multiple times (`#760 <https://github.com/gorakhargosh/watchdog/pull/760>`_)
1314
- [mac] Support coalesced filesystem events (`#734 <https://github.com/gorakhargosh/watchdog/pull/734>`_)
1415
- [mac] Drop support for OSX 10.12 and earlier (`#750 <https://github.com/gorakhargosh/watchdog/pull/750>`_)
1516
- [mac] Fix an issue when renaming an item changes only the casing (`#750 <https://github.com/gorakhargosh/watchdog/pull/750>`_)

src/watchdog/observers/inotify.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def on_thread_start(self):
120120
def on_thread_stop(self):
121121
if self._inotify:
122122
self._inotify.close()
123+
self._inotify = None
123124

124125
def queue_events(self, timeout, full_events=False):
125126
# If "full_events" is true, then the method will report unmatched move events as separate events
@@ -179,7 +180,8 @@ def queue_events(self, timeout, full_events=False):
179180
# cls = FileClosedEvent
180181
# self.queue_event(cls(src_path))
181182
elif event.is_delete_self and src_path == self.watch.path:
182-
self.queue_event(DirDeletedEvent(src_path))
183+
cls = DirDeletedEvent if event.is_directory else FileDeletedEvent
184+
self.queue_event(cls(src_path))
183185
self.stop()
184186

185187
def _decode_path(self, path):

src/watchdog/observers/inotify_c.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,12 @@ def close(self):
275275
if self._path in self._wd_for_path:
276276
wd = self._wd_for_path[self._path]
277277
inotify_rm_watch(self._inotify_fd, wd)
278-
os.close(self._inotify_fd)
278+
279+
try:
280+
os.close(self._inotify_fd)
281+
except OSError:
282+
# descriptor may be invalid because file was deleted
283+
pass
279284

280285
def read_events(self, event_buffer_size=DEFAULT_EVENT_BUFFER_SIZE):
281286
"""

tests/test_emitter.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ def setup_teardown(tmpdir):
7070

7171
yield
7272

73-
try:
74-
emitter.stop()
75-
except OSError:
76-
# watch was already stopped, e.g., in `test_delete_self`
77-
pass
73+
emitter.stop()
7874
emitter.join(5)
7975
assert not emitter.is_alive()
8076

tests/test_inotify_c.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ def watching(path=None, use_full_emitter=False):
4040
emitter = Emitter(event_queue, ObservedWatch(path, recursive=True))
4141
emitter.start()
4242
yield
43-
try:
44-
emitter.stop()
45-
except OSError:
46-
# watch was already stopped, e.g., because root was deleted
47-
pass
43+
emitter.stop()
4844
emitter.join(5)
4945

5046

0 commit comments

Comments
 (0)