Skip to content

Commit

Permalink
Fix adding failed emitters on observer schedule (#872)
Browse files Browse the repository at this point in the history
* Moved `_add_emitter` call post emitter start so it isn't called if the emitter fails to start

* Added test
  • Loading branch information
IlayRosenberg committed May 13, 2022
1 parent 030fb0a commit 0ab956e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/watchdog/observers/api.py
Expand Up @@ -298,9 +298,9 @@ def schedule(self, event_handler, path, recursive=False):
emitter = self._emitter_class(event_queue=self.event_queue,
watch=watch,
timeout=self.timeout)
self._add_emitter(emitter)
if self.is_alive():
emitter.start()
self._add_emitter(emitter)
self._watches.add(watch)
return watch

Expand Down
19 changes: 19 additions & 0 deletions tests/test_observer.py
Expand Up @@ -140,3 +140,22 @@ def mocked_start():
# Re-schduling the watch should work
observer.schedule(None, '')
assert len(observer.emitters) == 1


def test_schedule_failure_should_not_prevent_future_schedules(monkeypatch, observer):
observer.start()

# Make the emitter fail on start(), and subsequently the observer to fail on schedule()
def mocked_start(emitter):
raise OSError()
monkeypatch.setattr(EventEmitter, "start", mocked_start)

with pytest.raises(OSError):
observer.schedule(None, '')
# The emitter should not be in the list
emitters = observer.emitters
assert len(emitters) == 0

# Re-schduling the watch should work
observer.schedule(None, '')
assert len(observer.emitters) == 1

0 comments on commit 0ab956e

Please sign in to comment.