Skip to content

Commit 7906eb5

Browse files
committed
fix bug where we were unwatching everything but 256 files, instead of just unwatching 256 files
Unwatching occurs when we hit the syscall.EMFILE error ("too many open files"), and we need to unwatch so that we have some file descriptors available for starting commands i.e. cmd.Start(). On macOS, creating a timer for debouncing already consumes a file descriptor [1], and if there are no file descriptors available it panics with a very intimidating stack trace. [1] https://dzone.com/articles/go-servers-understanding-epoll-kqueue-netpoll#:~:text=kqueue%20is%20a%20similar%20mechanism%2C%20where%20registered%20events%20(read/write%2C%20timers%2C%20signals%2C%20files)%20are%20handled%20via%20kevent
1 parent f3bb5ed commit 7906eb5

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

wgo_cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ func (wgoCmd *WgoCmd) addDirsRecursively(watcher *fsnotify.Watcher, dir string)
663663
if unwatchCount > len(watchList)/2 {
664664
unwatchCount = int(0.2 * float64(len(watchList)))
665665
}
666-
for i := len(watchList) - 1; i >= unwatchCount; i-- {
666+
for i := len(watchList) - unwatchCount; i < len(watchList); i++ {
667667
watcher.Remove(watchList[i])
668668
}
669669
wgoCmd.Logger.Printf("ERROR too many open files (%d directories), not watching any more\n", len(watchList))
@@ -692,7 +692,7 @@ func (wgoCmd *WgoCmd) addDirsRecursively(watcher *fsnotify.Watcher, dir string)
692692
if unwatchCount > len(watchList)/2 {
693693
unwatchCount = int(0.2 * float64(len(watchList)))
694694
}
695-
for i := len(watchList) - 1; i >= unwatchCount; i-- {
695+
for i := len(watchList) - unwatchCount; i < len(watchList); i++ {
696696
watcher.Remove(watchList[i])
697697
}
698698
wgoCmd.Logger.Printf("ERROR too many open files (%d directories), not watching any more\n", len(watchList))

0 commit comments

Comments
 (0)