Skip to content

windows: lock watch field updates against concurrent WatchList#749

Merged
mattn merged 1 commit into
fsnotify:mainfrom
mattn:fix/issue-709-windows-watchlist-race
Apr 27, 2026
Merged

windows: lock watch field updates against concurrent WatchList#749
mattn merged 1 commit into
fsnotify:mainfrom
mattn:fix/issue-709-windows-watchlist-race

Conversation

@mattn

@mattn mattn commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #709. Calling WatchList() concurrently with Add()/Remove() on Windows raced with the I/O thread mutating watchEntry.mask and watchEntry.names outside the lock; the race detector caught the mask access but the more dangerous case is concurrent map iteration vs. write on watchEntry.names, which can panic at runtime. Hold w.mu around all writes to those fields (addWatch, remWatch, deleteWatch, and the rename/remove paths in readEvents). deleteWatch snapshots the map and mask under the lock and then calls sendEvent outside it to avoid blocking on the Events channel while holding the mutex. Adds TestWatchListRace as a regression test.

…ify#709)

WatchList() iterates watchEntry.names and reads watchEntry.mask under
w.mu, but the I/O thread (addWatch/remWatch/deleteWatch and the rename
and remove paths in readEvents) mutated those same fields without the
lock. The race detector flagged the mask field; the names map mutation
is the more dangerous case since concurrent map write + iterate panics
at runtime.

Hold w.mu around all writes to mask/names. deleteWatch snapshots the
map and mask under the lock and sends events outside it to avoid
blocking on the user-facing Events channel while holding the mutex.

Regression from v1.9.0.
@mattn mattn requested a review from shogo82148 April 26, 2026 13:47

@shogo82148 shogo82148 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ヨシッ

@mattn mattn merged commit dab9dde into fsnotify:main Apr 27, 2026
18 checks passed
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.

Race between Add() and WatchList() on Windows since v1.9.0

2 participants