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

Bugfix/dont add failed emitters on observer schedule #872

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -143,3 +143,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