diff --git a/src/django_watchfiles/__init__.py b/src/django_watchfiles/__init__.py index 7e7bd28..463c8c6 100644 --- a/src/django_watchfiles/__init__.py +++ b/src/django_watchfiles/__init__.py @@ -37,6 +37,8 @@ def stop(self) -> None: def __iter__(self) -> Generator[set[tuple[Change, str]]]: while True: + if self.stop_event.is_set(): + return self.change_event.clear() for changes in watch( *self.roots, diff --git a/tests/test_django_watchfiles.py b/tests/test_django_watchfiles.py index a5293a0..2d50668 100644 --- a/tests/test_django_watchfiles.py +++ b/tests/test_django_watchfiles.py @@ -4,6 +4,7 @@ import time from pathlib import Path +import pytest from django.utils import autoreload from watchfiles import Change @@ -31,9 +32,20 @@ def test_set_roots_changed(self): assert self.watcher.change_event.is_set() def test_stop(self): - assert not self.watcher.stop_event.is_set() + (self.temp_path / "test.txt").touch() + self.watcher.set_roots({self.temp_path}) + iterator = iter(self.watcher) + # Flush initial events + next(iterator) + self.watcher.stop() - assert self.watcher.stop_event.is_set() + + with pytest.raises(StopIteration): + next(iterator) + + # Not possible to restart + with pytest.raises(StopIteration): + next(iter(self.watcher)) def test_iter_no_changes(self): (self.temp_path / "test.txt").touch()