Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Version 3.0.5

Unreleased

- The Watchdog reloader ignores file closed no write events. :issue:`2945`


Version 3.0.4
-------------
Expand Down
15 changes: 13 additions & 2 deletions src/werkzeug/_reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ def run_step(self) -> None:

class WatchdogReloaderLoop(ReloaderLoop):
def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
from watchdog.events import EVENT_TYPE_OPENED
from watchdog.events import EVENT_TYPE_CLOSED
from watchdog.events import EVENT_TYPE_CREATED
from watchdog.events import EVENT_TYPE_DELETED
from watchdog.events import EVENT_TYPE_MODIFIED
from watchdog.events import EVENT_TYPE_MOVED
from watchdog.events import FileModifiedEvent
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
Expand All @@ -322,7 +326,14 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:

class EventHandler(PatternMatchingEventHandler):
def on_any_event(self, event: FileModifiedEvent): # type: ignore
if event.event_type == EVENT_TYPE_OPENED:
if event.event_type not in {
EVENT_TYPE_CLOSED,
EVENT_TYPE_CREATED,
EVENT_TYPE_DELETED,
EVENT_TYPE_MODIFIED,
EVENT_TYPE_MOVED,
}:
# skip events that don't involve changes to the file
return

trigger_reload(event.src_path)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from unittest.mock import patch

import pytest
from watchdog import version as watchdog_version
from watchdog.events import EVENT_TYPE_MODIFIED
from watchdog.events import EVENT_TYPE_OPENED
from watchdog.events import FileModifiedEvent
Expand Down Expand Up @@ -136,6 +137,28 @@ def test_watchdog_reloader_ignores_opened(mock_trigger_reload):
reloader.trigger_reload.assert_not_called()


@pytest.mark.skipif(
watchdog_version.VERSION_MAJOR < 5,
reason="'closed no write' event introduced in watchdog 5.0",
)
@patch.object(WatchdogReloaderLoop, "trigger_reload")
def test_watchdog_reloader_ignores_closed_no_write(mock_trigger_reload):
from watchdog.events import EVENT_TYPE_CLOSED_NO_WRITE

reloader = WatchdogReloaderLoop()
modified_event = FileModifiedEvent("")
modified_event.event_type = EVENT_TYPE_MODIFIED
reloader.event_handler.on_any_event(modified_event)
mock_trigger_reload.assert_called_once()

reloader.trigger_reload.reset_mock()

opened_event = FileModifiedEvent("")
opened_event.event_type = EVENT_TYPE_CLOSED_NO_WRITE
reloader.event_handler.on_any_event(opened_event)
reloader.trigger_reload.assert_not_called()


@pytest.mark.skipif(sys.version_info >= (3, 10), reason="not needed on >= 3.10")
def test_windows_get_args_for_reloading(monkeypatch, tmp_path):
argv = [str(tmp_path / "test.exe"), "run"]
Expand Down