Skip to content

Commit f3bb5ed

Browse files
committed
make unwatching when we hit "too many open files" deterministic
Also clarify in the log message that it's the number of directories watched, not number of files watched. We don't actually know the number of files watched since we only add directories to the watcher. On kqueue (macOS) that translates to one file descriptor used per directory and one file decriptor used per file, while on inotify (Linux) only needs one file descriptor per directory (https://news.ycombinator.com/item?id=9063910).
1 parent d98ecbe commit f3bb5ed

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

wgo_cmd.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path/filepath"
1515
"regexp"
1616
"runtime"
17+
"sort"
1718
"strconv"
1819
"strings"
1920
"sync"
@@ -657,14 +658,15 @@ func (wgoCmd *WgoCmd) addDirsRecursively(watcher *fsnotify.Watcher, dir string)
657658
if err != nil {
658659
if errors.Is(err, syscall.EMFILE) {
659660
watchList := watcher.WatchList()
661+
sort.Strings(watchList)
660662
unwatchCount := 256
661663
if unwatchCount > len(watchList)/2 {
662664
unwatchCount = int(0.2 * float64(len(watchList)))
663665
}
664666
for i := len(watchList) - 1; i >= unwatchCount; i-- {
665667
watcher.Remove(watchList[i])
666668
}
667-
wgoCmd.Logger.Printf("ERROR too many open files (%d open files), not watching any more\n", len(watchList))
669+
wgoCmd.Logger.Printf("ERROR too many open files (%d directories), not watching any more\n", len(watchList))
668670
return fs.SkipAll
669671
}
670672
return fs.SkipDir
@@ -685,14 +687,15 @@ func (wgoCmd *WgoCmd) addDirsRecursively(watcher *fsnotify.Watcher, dir string)
685687
if err != nil {
686688
if errors.Is(err, syscall.EMFILE) {
687689
watchList := watcher.WatchList()
690+
sort.Strings(watchList)
688691
unwatchCount := 256
689692
if unwatchCount > len(watchList)/2 {
690693
unwatchCount = int(0.2 * float64(len(watchList)))
691694
}
692695
for i := len(watchList) - 1; i >= unwatchCount; i-- {
693696
watcher.Remove(watchList[i])
694697
}
695-
wgoCmd.Logger.Printf("ERROR too many open files (%d open files), not watching any more\n", len(watchList))
698+
wgoCmd.Logger.Printf("ERROR too many open files (%d directories), not watching any more\n", len(watchList))
696699
return fs.SkipAll
697700
}
698701
return fs.SkipDir

0 commit comments

Comments
 (0)