kqueue: drop watches directly in Close() instead of going through remove()#740
Merged
Merged
Conversation
…ove() Close() called w.Remove(name) in a loop after w.shared.close() marked the watcher closed, but w.Remove -> w.remove short-circuits on w.isClosed() and returns nil without calling unix.Close on the watch fd or touching the watches bookkeeping. Every directory (and each file inside it - a dir watch opens one fd per child on macOS) leaked its descriptor on Close, so long-running processes that recycle watchers (hot-reload dev servers, test harnesses) hit EMFILE after a few cycles (fsnotify#732). Iterate the paths directly in Close(): register the EV_DELETE kevent, unix.Close the fd, and drop the watches entry. The pre-existing kqueue.remove() path is unchanged, so Remove() on a live watcher keeps the same semantics; only Close() stops leaning on it. Adds TestCloseClearsWatchState: creates a watcher over a dir + file, calls Close, and asserts every watches map is drained. Fails on master (3 path entries, 3 wd entries, 2 byUser, 3 byDir remain) and passes with the fix. Fixes fsnotify#732 Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
mattn
reviewed
Apr 22, 2026
| for _, name := range pathsToRemove { | ||
| w.Remove(name) | ||
| info, ok := w.watches.byPath(name) | ||
| if !ok { |
Contributor
There was a problem hiding this comment.
if !ok {
w.watches.remove(0, name)
continue
}
mattn
suggested changes
Apr 22, 2026
Per @mattn's review on fsnotify#740: if the Close loop walks a name from w.path but byPath can't resolve it to a live w.wd entry, the w.path / w.byUser entries for that name are still live and would survive Close. Call w.watches.remove(0, name) on the not-ok branch so the lookup maps are left consistent even in that inconsistent-state corner case.
Contributor
Author
|
Thanks @mattn — applied your suggestion. The not-ok branch now calls |
mattn
approved these changes
Apr 22, 2026
mattn
left a comment
Contributor
There was a problem hiding this comment.
I'll look this in later again. Thank you
Contributor
|
Thank you |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #732.
kqueue.Close()callsw.Remove(name)in a loop afterw.shared.close()has marked the watcher closed.w.Removeroutes throughw.remove, which short-circuits onw.isClosed()and returns nil without callingunix.Closeon the watch fd or dropping the watches bookkeeping. Every directory watch (and each file inside it — a dir watch opens one fd per child on macOS) leaked its descriptor onClose, so long-running processes that recycle watchers (hot-reload dev servers, test harnesses) hitEMFILEafter a few cycles.Fix
Iterate the paths directly in
Close(): register theEV_DELETEkevent,unix.Closethe fd, and drop thewatchesentry.kqueue.remove()is unchanged, soRemove()on a live watcher keeps the same semantics; onlyClose()stops leaning on it.Tests
Adds
TestCloseClearsWatchStateinbackend_kqueue_test.go: creates a watcher over a temp dir + file, callsClose, and asserts everywatchesmap is drained.watches.path: 3 entries,watches.wd: 3 entries,watches.byUser: 2 entries,watches.byDir: 3 entries.go test -count=1 ./...green on darwin;go vetclean.