sam sync --watch: template watcher thread dies silently on PermissionError (Windows file-lock race) - infra sync stops while code sync keeps running
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
**Description**
During `sam sync --watch` on Windows, saving `template.yaml` with an editor that performs atomic writes can race with the watchdog's read of the file. `parse_yaml_file` raises `PermissionError: [Errno 13]`, the exception kills the observer thread (`Exception in thread Thread-1`), and **the infra/template watch dies silently**: code sync keeps working normally, so the user has no signal that template changes are no longer being synced. We hit this twice in a row (2/2 reproduction) in a single session.
**Steps to reproduce**
1. Windows 11, SAM app with `nodejs24.x` + esbuild (two functions).
2. `sam sync --watch --stack-name my-dev-stack`, wait for the initial sync to settle.
3. Save `template.yaml` using tooling that writes files atomically (temp-file + replace — a common editor strategy).
4. Observe the traceback below; from then on, template changes are ignored while code changes still sync.
**Expected result**
A short retry on the read (the write lock is transient, ms-scale), or a loud failure that stops/restarts the whole watch — not a half-alive state.
**Actual result**
```
Exception in thread Thread-1:
Traceback (most recent call last):
File "...\watchdog\observers\api.py", line 212, in run
self.dispatch_events(self.event_queue)
File "...\watchdog\observers\api.py", line 386, in dispatch_events
handler.dispatch(event)
File "...\watchdog\events.py", line 447, in dispatch
super().dispatch(event)
File "...\watchdog\events.py", line 217, in dispatch
self.on_any_event(event)
File "...\samcli\lib\utils\resource_trigger.py", line 150, in _validator_wrapper
if self._validator.validate_change():
File "...\samcli\lib\utils\definition_validator.py", line 58, in validate_change
if not self.validate_file():
File "...\samcli\lib\utils\definition_validator.py", line 78, in validate_file
self._data = parse_yaml_file(str(self._path))
File "...\samcli\yamlhelper.py", line 148, in parse_yaml_file
with open(file_path, "r", encoding="utf-8") as fp:
PermissionError: [Errno 13] Permission denied: 'C:\\work\\my-app\\engineering\\template.yaml'
```
After this, `SyncFlow` messages continue for Lambda functions (code sync alive), but no infra sync is ever triggered by template edits.
**Workaround**
Restart `sam sync` — the boot detects the pending template change and applies it.
**Additional environment details**
- SAM CLI version: 1.165.0 (MSI install)
- OS: Windows 11
- Python: bundled with the MSI
- Project: `nodejs24.x`, arm64, esbuild builder
Contributor guide
Research direction
Start with samcli/lib/utils/resource_trigger.py and samcli/lib/utils/definition_validator.py, following the stack trace into samcli/yamlhelper.py and the file read in parse_yaml_file. Reproduce the PermissionError during sam sync --watch on Windows, then verify that a transient template write lock no longer silently ends template watching and that subsequent template edits trigger infra sync.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100