diff --git a/CHANGELOG.md b/CHANGELOG.md index ef3c33f9cf..57520f501f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `dash` will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/). +## [UNRELEASED] + +### Fixed +- [#4171](https://github.com/plotly/dash/pull/4171) Hot reload no longer stops for the rest of the session when the assets watcher finds a file it cannot stat, such as a dangling symlink (an Emacs lock file) or a temp file deleted mid-scan. Fixes [#4170](https://github.com/plotly/dash/issues/4170). + ## [4.5.1] - 2026-11-19 ### Removed diff --git a/dash/_watch.py b/dash/_watch.py index c13d70f7a6..9efb00c6a3 100644 --- a/dash/_watch.py +++ b/dash/_watch.py @@ -17,8 +17,12 @@ def walk(): continue path = os.path.join(current, f) - info = os.stat(path) - new_time = info.st_mtime + try: + new_time = os.stat(path).st_mtime + except OSError: + # Gone since os.walk listed it, or a dangling symlink + # such as an editor lock file. + continue if new_time > watched[path] > 0: on_change(path, new_time, False) diff --git a/tests/integration/devtools/test_hot_reload.py b/tests/integration/devtools/test_hot_reload.py index 9a8aeac481..1f84fae74f 100644 --- a/tests/integration/devtools/test_hot_reload.py +++ b/tests/integration/devtools/test_hot_reload.py @@ -1,6 +1,9 @@ import os +import sys from time import sleep +import pytest + from dash.testing.wait import until from dash import Dash, Input, Output, html @@ -129,3 +132,35 @@ def new_text(n): # rerenders with debug menu closed after reload # reopen and check that server is now available dash_duo_mp.find_element(".dash-debug-menu__button--available") + + +@pytest.mark.skipif(sys.platform == "win32", reason="symlinks need privileges") +def test_dvhr002_hot_reload_survives_dangling_symlink(dash_duo_mp, tmp_path): + assets = tmp_path / "assets" + assets.mkdir() + css = assets / "style.css" + css.write_text("#hot-reload-content { background-color: blue; }") + + app = Dash(__name__, assets_folder=str(assets)) + app.layout = html.Div("Hot reload", id="hot-reload-content") + + dash_duo_mp.start_server( + app, + dev_tools_hot_reload=True, + dev_tools_hot_reload_interval=0.1, + dev_tools_hot_reload_watch_interval=0.1, + dev_tools_hot_reload_max_retry=100, + ) + dash_duo_mp.wait_for_style_to_equal( + "#hot-reload-content", "background-color", "rgba(0, 0, 255, 1)" + ) + + # Editor lock files such as Emacs' `.#style.css` are dangling symlinks, + # so `os.stat` raises FileNotFoundError on them. + os.symlink(str(assets / "missing"), str(assets / ".#style.css")) + sleep(1) # let the watcher see it, and ensure a new mod time + + css.write_text("#hot-reload-content { background-color: red; }") + dash_duo_mp.wait_for_style_to_equal( + "#hot-reload-content", "background-color", "rgba(255, 0, 0, 1)" + )