From 5d163c2259acc312d2fef4862d7a60177b000226 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:13:24 -0800 Subject: [PATCH] Fix kqueue descriptor ownership across watch replacement Subscriptions can retain stale descriptor numbers after replacement. Closing them can close unrelated files and leak current descriptors. Path-only invalidation also loses failed-open entries and can terminate watches already attached to replacement inodes. Mixing link and target identities can also invalidate unchanged symlink watches. Keep entry ownership separate from kernel subscriptions and record file identity when opening descriptors. Clean only affected old trees, including failed-open entries. Follow symlink targets consistently while preserving dangling-link cleanup, replacement watches and overlaps. --- tsc/internal/fswatch/kqueue.go | 311 +++++++-------------- tsc/internal/fswatch/kqueue_test.go | 415 ++++++++++++++++++++++++++++ 2 files changed, 522 insertions(+), 204 deletions(-) create mode 100644 tsc/internal/fswatch/kqueue_test.go diff --git a/tsc/internal/fswatch/kqueue.go b/tsc/internal/fswatch/kqueue.go index ae3807c179102..6376b1068e14b 100644 --- a/tsc/internal/fswatch/kqueue.go +++ b/tsc/internal/fswatch/kqueue.go @@ -48,7 +48,7 @@ import ( // dispatch (compareDir, handleFileEvent) executes on this goroutine. // compareDir and handleFileEvent acquire b.mu for watch/fd lookups. // - subscribe/closeWatch run on the caller's goroutine under -// watcherBase.mu. watchPath acquires b.mu to register fd mappings. +// watcherBase.mu. subscribe acquires b.mu to register fd mappings. // // Callback delivery: // dirWatch.notify() posts to the shared process-wide debouncer. After a @@ -98,7 +98,18 @@ type dirEntry struct { path string watchPath string isDir bool - state any // stores the open fd + state any // owns the current open fd, or nil after it is closed + identity kqueueIdentity +} + +type kqueueIdentity struct { + device uint64 + inode uint64 +} + +// The native device number type differs across kqueue platforms. +func newKqueueIdentity[D ~int32 | ~uint32 | ~int64 | ~uint64](device D, inode uint64) kqueueIdentity { + return kqueueIdentity{device: uint64(device), inode: inode} } // kqueueSubscription. @@ -106,7 +117,6 @@ type kqueueSubscription struct { dirWatch *dirWatch path string entries map[string]*dirEntry - fd int } // kqueueBackend. It embeds treeReaderBackend (via Go @@ -119,11 +129,12 @@ type kqueueBackend struct { // pipeFDs[0] is read in the Start goroutine only. pipeFDs[1] is written // by Shutdown (any goroutine) to wake the loop, so it lives in // pipeWriteFD as an atomic with a sentinel of -1 once closed. - pipeFDs [2]int - pipeWriteFD atomic.Int32 - subsByPath map[string][]*kqueueSubscription // multimap - fdToEntry map[int]*dirEntry - endedSignal chan struct{} + pipeFDs [2]int + pipeWriteFD atomic.Int32 + subsByPath map[string][]*kqueueSubscription // multimap + fdToEntry map[int]*dirEntry + entriesByWatch map[*dirWatch]map[string]*dirEntry + endedSignal chan struct{} // Persistent buffer reused across event batches. Only accessed // from the start goroutine, so no synchronization needed. @@ -140,6 +151,7 @@ func newKqueueBackend() *kqueueBackend { pipeFDs: [2]int{-1, -1}, subsByPath: map[string][]*kqueueSubscription{}, fdToEntry: map[int]*dirEntry{}, + entriesByWatch: map[*dirWatch]map[string]*dirEntry{}, endedSignal: make(chan struct{}), watchersTouched: make(map[*dirWatch]struct{}), } @@ -257,21 +269,12 @@ func (b *kqueueBackend) closeFDs() { func (b *kqueueBackend) closeSubscriptions() { b.mu.Lock() - seenFDs := map[int]struct{}{} - for _, list := range b.subsByPath { - for _, sub := range list { - if sub.fd < 0 { - continue - } - if _, ok := seenFDs[sub.fd]; ok { - continue - } - seenFDs[sub.fd] = struct{}{} - _ = unix.Close(sub.fd) - } + for _, entries := range b.entriesByWatch { + b.cleanupEntriesLocked(entries) } b.subsByPath = map[string][]*kqueueSubscription{} b.fdToEntry = map[int]*dirEntry{} + b.entriesByWatch = map[*dirWatch]map[string]*dirEntry{} b.mu.Unlock() } @@ -287,53 +290,26 @@ func (b *kqueueBackend) shutdown() { func (b *kqueueBackend) handleFileEvent(fflags uint32, entry *dirEntry, touched map[*dirWatch]struct{}) { b.mu.Lock() defer b.mu.Unlock() + if entry.state == nil { + return + } subs := b.findSubscriptionsLocked(entry.path) if fflags&(unix.NOTE_DELETE|unix.NOTE_RENAME|unix.NOTE_REVOKE) != 0 { // Close the stale fd; the watched inode is gone. - if oldFD, ok := entry.state.(int); ok { - unix.Close(oldFD) - delete(b.fdToEntry, oldFD) - entry.state = nil - } + b.closeEntryLocked(entry) recreated := false if fflags&unix.NOTE_DELETE != 0 && fflags&(unix.NOTE_RENAME|unix.NOTE_REVOKE) == 0 && !entry.isDir { recreated = b.tryRewatchLocked(entry) } - + if !recreated { + b.removePathLocked(entry, touched) + return + } for _, sub := range subs { touched[sub.dirWatch] = struct{}{} - if recreated { - sub.dirWatch.events.update(sub.path) - } else { - sub.dirWatch.events.remove(sub.path) - // If we lost a directory, walk the entries map and - // close every fd we had open for descendants. Some - // kernels (OpenBSD in particular) deliver only the - // parent's NOTE_DELETE/NOTE_RENAME and never fire - // NOTE_DELETE on the children; without this cleanup, - // modifying a file inside the moved tree later - // surfaces an event against the descendant's stale - // (pre-rename) path. We also emit a delete for each - // descendant we close, so callers don't miss those - // removals if the kernel didn't fire per-child events. - // (When the kernel does fire them, our follow-up - // handleFileEvent finds the fd already gone and is a - // no-op, so events.create's coalescing handles dups.) - if entry.isDir { - b.closeDescendantFDsLocked(sub.dirWatch, sub.entries, sub.path) - } - removeEntryAndDescendants(sub.entries, sub.path) - // Root-of-watch deletion: no more events can fire - // for this dirWatch. Tell the caller. - if sub.path == sub.dirWatch.dir { - sub.dirWatch.events.setError(fmt.Errorf("%w: watched directory removed", ErrWatchTerminated)) - } - } - } - if !recreated { - delete(b.subsByPath, entry.path) + sub.dirWatch.events.update(sub.path) } return } @@ -346,26 +322,42 @@ func (b *kqueueBackend) handleFileEvent(fflags uint32, entry *dirEntry, touched } } -// closeDescendantFDsLocked closes every fd attached to an entry whose -// path lives strictly under root, removing the kevent registration and -// the corresponding b.subsByPath / b.fdToEntry bookkeeping, and emits a -// delete event for each. Used when a directory's parent is lost -// (deleted, renamed away) and the kernel didn't propagate the loss to -// children. eventList coalesces against any per-child NOTE_DELETE that -// arrives later. -func (b *kqueueBackend) closeDescendantFDsLocked(w *dirWatch, entries map[string]*dirEntry, root string) { +// An ancestor's removal may be the only event the kernel reports. Clean +// the affected trees, including entries without descriptors, but preserve +// watches already attached to a replacement inode at the same path. +func (b *kqueueBackend) removePathLocked(removed *dirEntry, touched map[*dirWatch]struct{}) { + root := removed.path prefix := root + string(filepath.Separator) - for path, e := range entries { - if !strings.HasPrefix(path, prefix) { + for w, entries := range b.entriesByWatch { + affected := false + if entry := entries[root]; entry != nil { + affected = entry == removed || entry.state == nil || entry.identity == removed.identity + } else if strings.HasPrefix(w.dir, prefix) { + // A nested watch has no entry for the removed ancestor. + // Its root fd still identifies whether it follows the + // current path or belongs to the tree that moved away. + entry := entries[w.dir] + var st unix.Stat_t + affected = unix.Stat(entry.watchPath, &st) != nil || + entry.identity != newKqueueIdentity(st.Dev, st.Ino) + } + if !affected { continue } - if fd, ok := e.state.(int); ok { - unix.Close(fd) - delete(b.fdToEntry, fd) - e.state = nil + for path, entry := range entries { + if path != root && !strings.HasPrefix(path, prefix) { + continue + } + b.closeEntryLocked(entry) + b.removeSubsForEntryLocked(path, entry) + delete(entries, path) + w.events.remove(path) + touched[w] = struct{}{} + if path == w.dir { + w.events.setError(fmt.Errorf("%w: watched directory removed", ErrWatchTerminated)) + delete(b.entriesByWatch, w) + } } - delete(b.subsByPath, path) - w.events.remove(path) } } @@ -385,24 +377,31 @@ func (b *kqueueBackend) tryRewatchLocked(entry *dirEntry) bool { return false } + return b.openEntryLocked(entry) == nil +} + +func (b *kqueueBackend) openEntryLocked(entry *dirEntry) error { fd, err := openForEvents(entry.watchPath) if err != nil { - return false + return err + } + var st unix.Stat_t + if err := unix.Fstat(fd, &st); err != nil { + unix.Close(fd) + return err } - var ev unix.Kevent_t unix.SetKevent(&ev, fd, unix.EVFILT_VNODE, unix.EV_ADD|unix.EV_CLEAR|unix.EV_ENABLE) ev.Fflags = unix.NOTE_DELETE | unix.NOTE_WRITE | unix.NOTE_EXTEND | unix.NOTE_ATTRIB | unix.NOTE_RENAME | unix.NOTE_REVOKE if _, err := unix.Kevent(b.kq, []unix.Kevent_t{ev}, nil, nil); err != nil { unix.Close(fd) - return false + return err } - entry.state = fd - + entry.identity = newKqueueIdentity(st.Dev, st.Ino) b.fdToEntry[fd] = entry - return true + return nil } func (b *kqueueBackend) closeEntryLocked(entry *dirEntry) { @@ -413,11 +412,11 @@ func (b *kqueueBackend) closeEntryLocked(entry *dirEntry) { } } -func (b *kqueueBackend) removeSubsForEntriesLocked(path string, entriesPtr *map[string]*dirEntry) { +func (b *kqueueBackend) removeSubsForEntryLocked(path string, entry *dirEntry) { list := b.subsByPath[path] kept := list[:0] for _, sub := range list { - if &sub.entries == entriesPtr { + if sub.entries[path] == entry { continue } kept = append(kept, sub) @@ -429,22 +428,6 @@ func (b *kqueueBackend) removeSubsForEntriesLocked(path string, entriesPtr *map[ } } -func (b *kqueueBackend) removeEntryAndDescendantsLocked(entriesPtr *map[string]*dirEntry, path string, includeRoot bool) { - entries := *entriesPtr - for descendant, e := range entries { - if descendant == path { - if !includeRoot { - continue - } - } else if !(len(descendant) > len(path) && descendant[len(path)] == filepath.Separator && descendant[:len(path)] == path) { - continue - } - b.closeEntryLocked(e) - b.removeSubsForEntriesLocked(descendant, entriesPtr) - delete(entries, descendant) - } -} - func (b *kqueueBackend) findSubscriptionsLocked(path string) []*kqueueSubscription { subs := b.subsByPath[path] out := make([]*kqueueSubscription, len(subs)) @@ -476,8 +459,7 @@ func (b *kqueueBackend) subscribe(w *dirWatch) error { defer b.mu.Unlock() for path, entry := range entries { - fd, err := openForEvents(entry.watchPath) - if err != nil { + if err := b.openEntryLocked(entry); err != nil { if path == w.dir { b.cleanupEntriesLocked(entries) return &dirWatchError{ @@ -488,29 +470,11 @@ func (b *kqueueBackend) subscribe(w *dirWatch) error { delete(entries, path) continue } - var ev unix.Kevent_t - unix.SetKevent(&ev, fd, unix.EVFILT_VNODE, unix.EV_ADD|unix.EV_CLEAR|unix.EV_ENABLE) - ev.Fflags = unix.NOTE_DELETE | unix.NOTE_WRITE | unix.NOTE_EXTEND | - unix.NOTE_ATTRIB | unix.NOTE_RENAME | unix.NOTE_REVOKE - if _, err := unix.Kevent(b.kq, []unix.Kevent_t{ev}, nil, nil); err != nil { - unix.Close(fd) - if path == w.dir { - b.cleanupEntriesLocked(entries) - return &dirWatchError{ - err: fmt.Errorf("error watching %s: %w", w.dir, err), - dirWatch: w, - } - } - delete(entries, path) - continue - } - entry.state = fd - b.fdToEntry[fd] = entry } - for path, entry := range entries { - fd := entry.state.(int) - sub := &kqueueSubscription{dirWatch: w, path: path, entries: entries, fd: fd} + b.entriesByWatch[w] = entries + for path := range entries { + sub := &kqueueSubscription{dirWatch: w, path: path, entries: entries} b.subsByPath[path] = append(b.subsByPath[path], sub) } return nil @@ -520,41 +484,22 @@ func (b *kqueueBackend) subscribe(w *dirWatch) error { // Called on subscribe failure to avoid fd leaks. Must be called under b.mu. func (b *kqueueBackend) cleanupEntriesLocked(entries map[string]*dirEntry) { for _, e := range entries { - if fd, ok := e.state.(int); ok { - unix.Close(fd) - delete(b.fdToEntry, fd) - e.state = nil - } + b.closeEntryLocked(e) } } -// watchPath corresponds to `kqueueBackend::watchDir`. -func (b *kqueueBackend) watchPath(w *dirWatch, path string, entries map[string]*dirEntry) bool { +// watchPathLocked registers an entry while b.mu is held. +func (b *kqueueBackend) watchPathLocked(w *dirWatch, path string, entries map[string]*dirEntry) bool { entry := entries[path] if entry == nil { return false } - b.mu.Lock() - defer b.mu.Unlock() - sub := &kqueueSubscription{dirWatch: w, path: path, entries: entries} if entry.state == nil { - fd, err := openForEvents(entry.watchPath) - if err != nil { + if err := b.openEntryLocked(entry); err != nil { return false } - var ev unix.Kevent_t - unix.SetKevent(&ev, fd, unix.EVFILT_VNODE, unix.EV_ADD|unix.EV_CLEAR|unix.EV_ENABLE) - ev.Fflags = unix.NOTE_DELETE | unix.NOTE_WRITE | unix.NOTE_EXTEND | - unix.NOTE_ATTRIB | unix.NOTE_RENAME | unix.NOTE_REVOKE - if _, err := unix.Kevent(b.kq, []unix.Kevent_t{ev}, nil, nil); err != nil { - unix.Close(fd) - return false - } - entry.state = fd - b.fdToEntry[fd] = entry } - sub.fd = entry.state.(int) b.subsByPath[path] = append(b.subsByPath[path], sub) return true } @@ -564,8 +509,8 @@ func (b *kqueueBackend) watchPath(w *dirWatch, path string, entries map[string]* // create/remove events. func (b *kqueueBackend) compareDir(_ int, path string, touched map[*dirWatch]struct{}) bool { b.mu.Lock() + defer b.mu.Unlock() subs := b.findSubscriptionsLocked(path) - b.mu.Unlock() // For non-recursive subscriptions, only compareDir on the root dir. // NOTE_WRITE on a child dir means something changed inside it, but @@ -633,17 +578,18 @@ func (b *kqueueBackend) compareDir(_ int, path string, touched map[*dirWatch]str // (now unlinked) inode. if fd, ok := existing.state.(int); ok { var fdSt, pathSt unix.Stat_t - if unix.Fstat(fd, &fdSt) == nil && unix.Lstat(fullWatchPath, &pathSt) == nil { + // Event descriptors follow symlinks. Use the link's + // own identity only when its target cannot be resolved. + pathErr := unix.Stat(fullWatchPath, &pathSt) + if pathErr != nil { + pathErr = unix.Lstat(fullWatchPath, &pathSt) + } + if unix.Fstat(fd, &fdSt) == nil && pathErr == nil { if fdSt.Dev != pathSt.Dev || fdSt.Ino != pathSt.Ino { // Inode changed: path was replaced. - b.mu.Lock() - b.closeEntryLocked(existing) - b.removeSubsForEntriesLocked(fullPath, &sub.entries) - if existing.isDir { - b.removeEntryAndDescendantsLocked(&sub.entries, fullPath, false) - } + b.removePathLocked(existing, touched) + entries[fullPath] = existing existing.isDir = ent.IsDir() - b.mu.Unlock() } } } @@ -653,10 +599,10 @@ func (b *kqueueBackend) compareDir(_ int, path string, touched map[*dirWatch]str } // Entry exists but fd is stale: the file was replaced. // Re-watch it and emit an update. - if !b.watchPath(sub.dirWatch, fullPath, entries) { + if !b.watchPathLocked(sub.dirWatch, fullPath, entries) { continue } - sub.dirWatch.events.update(fullPath) + sub.dirWatch.events.create(fullPath) touched[sub.dirWatch] = struct{}{} if ent.IsDir() && sub.dirWatch.recursive { _ = walkDir(fullWatchPath, true, func(p string, pIsDir bool) error { @@ -667,7 +613,7 @@ func (b *kqueueBackend) compareDir(_ int, path string, touched map[*dirWatch]str e := &dirEntry{path: displayPath, watchPath: p, isDir: pIsDir} entries[displayPath] = e sub.dirWatch.events.create(displayPath) - b.watchPath(sub.dirWatch, displayPath, entries) + b.watchPathLocked(sub.dirWatch, displayPath, entries) return nil }) } @@ -675,7 +621,7 @@ func (b *kqueueBackend) compareDir(_ int, path string, touched map[*dirWatch]str } e := &dirEntry{path: fullPath, watchPath: fullWatchPath, isDir: ent.IsDir()} entries[fullPath] = e - if !b.watchPath(sub.dirWatch, fullPath, entries) { + if !b.watchPathLocked(sub.dirWatch, fullPath, entries) { delete(entries, fullPath) continue } @@ -694,7 +640,7 @@ func (b *kqueueBackend) compareDir(_ int, path string, touched map[*dirWatch]str entry := &dirEntry{path: displayPath, watchPath: p, isDir: pIsDir} entries[displayPath] = entry sub.dirWatch.events.create(displayPath) - b.watchPath(sub.dirWatch, displayPath, entries) + b.watchPathLocked(sub.dirWatch, displayPath, entries) return nil }) } @@ -717,21 +663,7 @@ func (b *kqueueBackend) compareDir(_ int, path string, touched map[*dirWatch]str toRemove = append(toRemove, p) } for _, p := range toRemove { - sub.dirWatch.events.remove(p) - touched[sub.dirWatch] = struct{}{} - b.mu.Lock() - for descendant, e := range entries { - if descendant != p && !(len(descendant) > len(p) && descendant[len(p)] == filepath.Separator && descendant[:len(p)] == p) { - continue - } - if fd, ok := e.state.(int); ok { - unix.Close(fd) - delete(b.fdToEntry, fd) - } - delete(b.subsByPath, descendant) - } - b.mu.Unlock() - removeEntryAndDescendants(entries, p) + b.removePathLocked(entries[p], touched) } } return true @@ -746,39 +678,10 @@ func readEntries(path string) ([]os.DirEntry, error) { func (b *kqueueBackend) closeWatch(w *dirWatch) error { b.mu.Lock() defer b.mu.Unlock() - for path, list := range b.subsByPath { - kept := list[:0] - removedAny := false - for _, s := range list { - if s.dirWatch == w { - removedAny = true - continue - } - kept = append(kept, s) - } - if !removedAny { - continue - } - if len(kept) == 0 { - // Closing the file descriptor automatically unwatches it in kqueue. - fd := list[0].fd - unix.Close(fd) - delete(b.fdToEntry, fd) - delete(b.subsByPath, path) - } else { - b.subsByPath[path] = kept - } + for path, entry := range b.entriesByWatch[w] { + b.closeEntryLocked(entry) + b.removeSubsForEntryLocked(path, entry) } + delete(b.entriesByWatch, w) return nil } - -// removeEntryAndDescendants removes path and all paths prefixed with -// path + separator from the entries map. -func removeEntryAndDescendants(entries map[string]*dirEntry, path string) { - delete(entries, path) - for k := range entries { - if len(k) > len(path) && k[len(path)] == filepath.Separator && k[:len(path)] == path { - delete(entries, k) - } - } -} diff --git a/tsc/internal/fswatch/kqueue_test.go b/tsc/internal/fswatch/kqueue_test.go new file mode 100644 index 0000000000000..a0dbbb8bd87c4 --- /dev/null +++ b/tsc/internal/fswatch/kqueue_test.go @@ -0,0 +1,415 @@ +//go:build darwin || freebsd || openbsd || netbsd || dragonfly + +package fswatch + +import ( + "errors" + "os" + "path/filepath" + "testing" + "time" + + "golang.org/x/sys/unix" +) + +func newTestKqueueWatcher(t *testing.T) (Watcher, *kqueueBackend) { + t.Helper() + impl := newKqueueBackend() + t.Cleanup(impl.shutdown) + return &watcher{name: "kqueue", factory: func() watcherImpl { return impl }}, impl +} + +func openKqueueTestFile(t *testing.T, path string) *os.File { + t.Helper() + file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = file.Close() }) + return file +} + +// Find the descriptor opened by a subscription using its inode, not backend +// bookkeeping. The caller keeps a file open below the subscription's fds. +func kqueueTestDescriptor(t *testing.T, path string, lower int) int { + t.Helper() + upper, err := os.Open(os.DevNull) + if err != nil { + t.Fatal(err) + } + defer upper.Close() + var want unix.Stat_t + if err := unix.Stat(path, &want); err != nil { + t.Fatal(err) + } + found := -1 + for fd := lower + 1; fd < int(upper.Fd()); fd++ { + var st unix.Stat_t + if unix.Fstat(fd, &st) == nil && st.Dev == want.Dev && st.Ino == want.Ino { + if found != -1 { + t.Fatalf("multiple descriptors for %s", path) + } + found = fd + } + } + if found == -1 { + t.Fatalf("no descriptor for %s", path) + } + return found +} + +func requireKqueueDescriptorClosed(t *testing.T, fd int) { + t.Helper() + var st unix.Stat_t + if err := unix.Fstat(fd, &st); !errors.Is(err, unix.EBADF) { + t.Errorf("descriptor %d was not released: %v", fd, err) + } +} + +func TestKqueueReplacedFileDescriptorLifetime(t *testing.T) { //nolint:paralleltest // Controls descriptor reuse within this process. + for _, shutdown := range []bool{false, true} { //nolint:paralleltest // Controls descriptor reuse within this process. + name := "close-watch" + if shutdown { + name = "shutdown" + } + t.Run(name, func(t *testing.T) { + w, impl := newTestKqueueWatcher(t) + root := newTmpDir(t) + // Start the backend before reserving a lower descriptor slot. + _, anchor := subscribeFor(t, newTmpDir(t), w) + dir := filepath.Join(root, "watched") + if err := os.Mkdir(dir, 0o755); err != nil { + t.Fatal(err) + } + path := filepath.Join(dir, "file") + replacement := filepath.Join(root, "replacement") + for _, p := range []string{path, replacement} { + if err := os.WriteFile(p, []byte("before"), 0o600); err != nil { + t.Fatal(err) + } + } + spare := openKqueueTestFile(t, filepath.Join(root, "spare")) + spareFD := int(spare.Fd()) + rec, sub := subscribeFor(t, dir, w) + oldFD := kqueueTestDescriptor(t, path, spareFD) + + // Atomic replacement must rewatch the new inode. Freeing a lower + // slot ensures the rewatch cannot retain the old fd number. + if err := spare.Close(); err != nil { + t.Fatal(err) + } + if err := os.Rename(replacement, path); err != nil { + t.Fatal(err) + } + expectContains(t, rec, EventUpdate, path) + currentFD := kqueueTestDescriptor(t, path, spareFD-1) + if currentFD == oldFD { + t.Fatal("replacement did not reuse the lower descriptor slot") + } + if err := os.WriteFile(path, []byte("after"), 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, rec, EventUpdate, path) + + // Put an unrelated live file in the old descriptor slot. + var reused *os.File + for { + file := openKqueueTestFile(t, filepath.Join(root, "unrelated")) + if int(file.Fd()) > oldFD { + t.Fatal("old descriptor is still in use") + } + if int(file.Fd()) == oldFD { + reused = file + break + } + } + if shutdown { + impl.shutdown() + } else if err := sub.Close(); err != nil { + t.Fatal(err) + } + if _, err := reused.WriteString("still open"); err != nil { + t.Errorf("closing the watch closed an unrelated file: %v", err) + } + requireKqueueDescriptorClosed(t, currentFD) + if err := sub.Close(); err != nil { + t.Fatal(err) + } + if err := anchor.Close(); err != nil { + t.Fatal(err) + } + }) + } +} + +func TestKqueueRootReplacementDescriptorLifetime(t *testing.T) { //nolint:paralleltest // Controls descriptor reuse within this process. + for _, rename := range []bool{false, true} { //nolint:paralleltest // Controls descriptor reuse within this process. + name := "delete" + if rename { + name = "rename" + } + t.Run(name, func(t *testing.T) { + w, _ := newTestKqueueWatcher(t) + root := newTmpDir(t) + dir := filepath.Join(root, "watched") + if err := os.Mkdir(dir, 0o755); err != nil { + t.Fatal(err) + } + parentRec, parent := subscribeFor(t, root, w) + unrelated := newTmpDir(t) + lower := openKqueueTestFile(t, filepath.Join(unrelated, "lower")) + terminated := make(chan error, 1) + sub, err := w.WatchDirectory(dir, func(_ []Event, err error) { + if err != nil { + select { + case terminated <- err: + return + default: + return + } + } + }) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = sub.Close() }) + oldFD := kqueueTestDescriptor(t, dir, int(lower.Fd())) + if rename { + err = os.Rename(dir, filepath.Join(unrelated, "moved")) + } else { + err = os.Remove(dir) + } + if err != nil { + t.Fatal(err) + } + select { + case err := <-terminated: + if !errors.Is(err, ErrWatchTerminated) { + t.Fatalf("unexpected root error: %v", err) + } + case <-time.After(watcherEventTimeout(t, w)): + t.Fatal("root watch did not terminate") + } + expectContains(t, parentRec, EventDelete, dir) + requireKqueueDescriptorClosed(t, oldFD) + if err := sub.Close(); err != nil { + t.Fatal(err) + } + if err := os.Mkdir(dir, 0o755); err != nil { + t.Fatal(err) + } + expectContains(t, parentRec, EventUpdate, dir) + parentFD := kqueueTestDescriptor(t, dir, -1) + + lowerNew := openKqueueTestFile(t, filepath.Join(unrelated, "lower-new")) + rec, replacement := subscribeFor(t, dir, w) + currentFD := kqueueTestDescriptor(t, dir, int(lowerNew.Fd())) + if err := parent.Close(); err != nil { + t.Fatal(err) + } + requireKqueueDescriptorClosed(t, parentFD) + file := filepath.Join(dir, "new-file") + if err := os.WriteFile(file, []byte("after"), 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, rec, EventUpdate, file) + if err := replacement.Close(); err != nil { + t.Fatal(err) + } + requireKqueueDescriptorClosed(t, currentFD) + }) + } +} + +func TestKqueueSymlinkWatchSurvivesParentChange(t *testing.T) { + t.Parallel() + w, _ := newTestKqueueWatcher(t) + root := newTmpDir(t) + target := newTmpDir(t) + link := filepath.Join(root, "link") + if err := os.Symlink(target, link); err != nil { + t.Fatal(err) + } + parentRec, parent := subscribeFor(t, root, w) + linkRec, _ := subscribeFor(t, link, w) + + sibling := filepath.Join(root, "sibling") + if err := os.WriteFile(sibling, nil, 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, parentRec, EventUpdate, sibling) + file := filepath.Join(target, "file") + if err := os.WriteFile(file, []byte("before parent close"), 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, linkRec, EventUpdate, filepath.Join(link, "file")) + if err := parent.Close(); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(file, []byte("after parent close"), 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, linkRec, EventUpdate, filepath.Join(link, "file")) + linkRec.mu.Lock() + defer linkRec.mu.Unlock() + if len(linkRec.errs) != 0 { + t.Fatalf("unchanged symlink watch received errors: %v", linkRec.errs) + } +} + +func TestKqueueUnopenedRecursiveEntryRemoval(t *testing.T) { + t.Parallel() + w, _ := newTestKqueueWatcher(t) + root := newTmpDir(t) + rec, _ := subscribeFor(t, root, w) + staging := filepath.Join(newTmpDir(t), "staging") + if err := os.Mkdir(staging, 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(staging, "missing"), filepath.Join(staging, "dangling")); err != nil { + t.Fatal(err) + } + destination := filepath.Join(root, "subtree") + if err := os.Rename(staging, destination); err != nil { + t.Fatal(err) + } + link := filepath.Join(destination, "dangling") + expectContains(t, rec, EventUpdate, link) + if err := os.Remove(link); err != nil { + t.Fatal(err) + } + expectContains(t, rec, EventDelete, link) + if err := os.WriteFile(link, []byte("replacement"), 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, rec, EventUpdate, link) +} + +func TestKqueueReplacementWatchBeforeParentDispatch(t *testing.T) { //nolint:paralleltest // Locates descriptors to control kernel event delivery. + w, impl := newTestKqueueWatcher(t) + _, _ = subscribeFor(t, newTmpDir(t), w) + root := newTmpDir(t) + child := filepath.Join(root, "child") + if err := os.Mkdir(child, 0o755); err != nil { + t.Fatal(err) + } + lower := openKqueueTestFile(t, filepath.Join(newTmpDir(t), "lower")) + parentRec, _ := subscribeFor(t, root, w) + rootFD := kqueueTestDescriptor(t, root, int(lower.Fd())) + childFD := kqueueTestDescriptor(t, child, int(lower.Fd())) + + // Hold actual kernel delivery while the public API subscribes to the + // replacement. No backend subscription bookkeeping is modified. + changes := make([]unix.Kevent_t, 2) + for i, fd := range []int{rootFD, childFD} { + unix.SetKevent(&changes[i], fd, unix.EVFILT_VNODE, unix.EV_DISABLE) + } + if _, err := unix.Kevent(impl.kq, changes, nil, nil); err != nil { + t.Fatal(err) + } + if err := os.Rename(child, filepath.Join(newTmpDir(t), "old-child")); err != nil { + t.Fatal(err) + } + if err := os.Mkdir(child, 0o755); err != nil { + t.Fatal(err) + } + childRec, _ := subscribeFor(t, child, w) + unix.SetKevent(&changes[0], rootFD, unix.EVFILT_VNODE, unix.EV_ENABLE) + if _, err := unix.Kevent(impl.kq, changes[:1], nil, nil); err != nil { + t.Fatal(err) + } + barrier := filepath.Join(root, "barrier") + if err := os.WriteFile(barrier, nil, 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, parentRec, EventUpdate, barrier) + + file := filepath.Join(child, "file") + if err := os.WriteFile(file, []byte("replacement"), 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, childRec, EventUpdate, file) + childRec.mu.Lock() + defer childRec.mu.Unlock() + if len(childRec.errs) != 0 { + t.Fatalf("replacement watch received errors: %v", childRec.errs) + } +} + +func TestKqueueAncestorRenameTerminatesNestedWatch(t *testing.T) { + t.Parallel() + for _, recursive := range []bool{false, true} { + name := "non-recursive" + if recursive { + name = "recursive" + } + t.Run(name, func(t *testing.T) { + t.Parallel() + w, _ := newTestKqueueWatcher(t) + root := newTmpDir(t) + ancestor := filepath.Join(root, "moved") + nested := filepath.Join(ancestor, "nested") + sibling := filepath.Join(root, "moved-other") + for _, dir := range []string{nested, sibling} { + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "file"), []byte("before"), 0o600); err != nil { + t.Fatal(err) + } + } + var opts []WatchOption + if recursive { + opts = append(opts, WithRecursive()) + } + parentRec, parent := subscribeForOpts(t, root, w, opts...) + nestedRec, nestedSub := subscribeFor(t, nested, w) + siblingRec, siblingSub := subscribeFor(t, sibling, w) + destination := filepath.Join(newTmpDir(t), "renamed") + if err := os.Rename(ancestor, destination); err != nil { + t.Fatal(err) + } + expectContains(t, parentRec, EventDelete, ancestor) + expectContains(t, nestedRec, EventDelete, nested) + nestedRec.mu.Lock() + terminated := false + for _, err := range nestedRec.errs { + terminated = terminated || errors.Is(err, ErrWatchTerminated) + } + nestedRec.mu.Unlock() + if !terminated { + t.Fatal("nested watch did not receive ErrWatchTerminated") + } + + if err := os.WriteFile(filepath.Join(destination, "nested", "file"), []byte("after"), 0o600); err != nil { + t.Fatal(err) + } + if events := nestedRec.next(watcherEventTimeout(t, w)); len(events) != 0 { + t.Fatalf("terminated watch delivered stale events: %v", events) + } + if err := nestedSub.Close(); err != nil { + t.Fatal(err) + } + if err := nestedSub.Close(); err != nil { + t.Fatal(err) + } + + file := filepath.Join(root, "parent-file") + if err := os.WriteFile(file, []byte("parent"), 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, parentRec, EventUpdate, file) + if err := parent.Close(); err != nil { + t.Fatal(err) + } + siblingFile := filepath.Join(sibling, "file") + if err := os.WriteFile(siblingFile, []byte("sibling"), 0o600); err != nil { + t.Fatal(err) + } + expectContains(t, siblingRec, EventUpdate, siblingFile) + if err := siblingSub.Close(); err != nil { + t.Fatal(err) + } + }) + } +}