Skip to content

fix: handle broken symlinks in filewatcher initial scan#2156

Open
abhu85 wants to merge 1 commit intoaws:masterfrom
abhu85:fix/filewatcher-broken-symlink-997
Open

fix: handle broken symlinks in filewatcher initial scan#2156
abhu85 wants to merge 1 commit intoaws:masterfrom
abhu85:fix/filewatcher-broken-symlink-997

Conversation

@abhu85
Copy link
Copy Markdown

@abhu85 abhu85 commented Feb 19, 2026

Summary

This PR fixes the file watcher crash when encountering broken symlinks during the initial cache seeding phase.

Problem: When a directory contains a symlink pointing to a non-existent file (such as Emacs lock files like .#app.py), the _seed_mtime_cache method would crash with a FileNotFoundError, making the file watcher unusable and preventing hot-reloading during development.

Solution: Add exception handling to _seed_mtime_cache to catch (OSError, IOError) when calling mtime(), consistent with the existing error handling in _is_changed_file. Files that cannot be stat'd are skipped with a debug log message.

Changes

  • chalice/cli/filewatch/stat.py: Added try/except block in _seed_mtime_cache to handle broken symlinks
  • tests/unit/cli/filewatch/test_stat.py: Added regression test test_can_handle_broken_symlink_during_initial_scan

Reproduction

# Create a broken symlink (like Emacs creates)
ln -s /nonexistent/path .#app.py
# Run chalice local - this would crash before this fix
chalice local

Test Plan

  • Unit tests pass (pytest tests/unit/cli/filewatch/test_stat.py)
  • New regression test added that specifically tests broken symlink handling during initial scan
  • Manual reproduction confirmed - broken symlinks are now skipped gracefully
  • Flake8 passes
  • Mypy passes with project configuration
  • Pylint score: 10.00/10

Fixes #997


Generated with assistance from Claude Code

Add exception handling to _seed_mtime_cache to handle FileNotFoundError
and other OS errors that can occur when scanning directories containing
broken symlinks (e.g., Emacs lock files like .#app.py).

Previously, the filewatcher would crash during initial cache seeding
when encountering a symlink pointing to a non-existent file. This made
the file watcher unusable, preventing hot-reloading during development.

The fix catches (OSError, IOError) in _seed_mtime_cache, consistent
with the existing error handling in _is_changed_file, and logs a debug
message for skipped files.

Fixes aws#997

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: abhu85 <user@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FileNotFound not properly handled in filewatcher

2 participants