diff --git a/internal/dirent/watch.go b/internal/dirent/watch.go index ccc32259a..36be32fc5 100644 --- a/internal/dirent/watch.go +++ b/internal/dirent/watch.go @@ -23,8 +23,9 @@ import ( // change made before it is heard: it writes a file of its own in a new // folder the watch covers, and the system tells changes in the order they were // made, so once that write is heard so is every one before it. Anything -// the watch can't vouch for (it dropped events, a root moved, a volume -// came or went, a wait ran out, it hasn't started) lists again, and so +// the watch can't vouch for (it dropped events, a root was moved or +// replaced, a volume came or went, a wait ran out, it hasn't started) +// lists again, and so // does a listing older than listedFor, should an event never come. var watched struct { sync.Mutex @@ -44,6 +45,7 @@ var watched struct { type watchRoot struct { given, real string dev uint64 + fi os.FileInfo // the folder the watch began on } // probe is the folder Watch writes in to know every change before is @@ -107,6 +109,9 @@ func WatchSince(dir string, mark int) { tried := watched.tried[dir] covered := watched.active && watched.vouched > mark watched.Unlock() + if tried && rootMoved(dir) { + tried = false + } if !tried { if !add(dir) { return @@ -118,6 +123,40 @@ func WatchSince(dir string, mark int) { } } +// rootMoved drops the watch of dir's root when the root is no longer the +// folder the watch began on, and is whether it did. FSEvents tells of a +// change in a folder under a root, not of the root itself renamed, +// removed or put back, so a tree moved in whole under the root's name is +// heard nowhere. starting is held. +func rootMoved(dir string) bool { + watched.Lock() + var r watchRoot + for _, x := range watched.roots { + if within(dir, x.given) { + r = x + break + } + } + watched.Unlock() + if r.fi == nil { + return false + } + if fi, err := os.Stat(r.given); err == nil && os.SameFile(fi, r.fi) { + return false + } + watched.Lock() + defer watched.Unlock() + watched.roots = slices.DeleteFunc(slices.Clone(watched.roots), func(x watchRoot) bool { return x.given == r.given }) + for d := range watched.tried { + if within(d, r.given) { + delete(watched.tried, d) + } + } + watched.seq++ + watched.all = watched.seq + return true +} + // starting is held while a watch is made or waited on, outside watched: // stopping a stream waits for its word, which takes watched. var starting sync.Mutex @@ -144,7 +183,7 @@ func add(dir string) bool { return true } } - roots := append(slices.Clip(watched.roots), watchRoot{dir, real, devOf(fi)}) + roots := append(slices.Clip(watched.roots), watchRoot{dir, real, devOf(fi), fi}) p := watched.probe watched.active = false watched.Unlock() diff --git a/internal/dirent/watch_darwin.go b/internal/dirent/watch_darwin.go index 96a56d1aa..a4a3e60bb 100644 --- a/internal/dirent/watch_darwin.go +++ b/internal/dirent/watch_darwin.go @@ -38,9 +38,12 @@ static int direntStart(char **paths, int n) { CFArrayAppendValue(list, p); CFRelease(p); } + // no kFSEventStreamCreateFlagWatchRoot: with it, files other code + // had open in the same process came back EBADF (#6368, #6785; 916 + // in ~1M operations on macOS 15 and 26 CI, none without it). WatchSince + // checks the roots itself. FSEventStreamRef s = FSEventStreamCreate(NULL, direntCallback, NULL, list, - kFSEventStreamEventIdSinceNow, 0.01, - kFSEventStreamCreateFlagNoDefer | kFSEventStreamCreateFlagWatchRoot); + kFSEventStreamEventIdSinceNow, 0.01, kFSEventStreamCreateFlagNoDefer); CFRelease(list); if (s == NULL) { return 0; @@ -65,12 +68,11 @@ import ( ) // FSEvents' flags that say a folder's word can't be taken alone: events -// were dropped or merged, a root moved, a volume came or went. +// were dropped or merged, a volume came or went. const heardAll = C.kFSEventStreamEventFlagMustScanSubDirs | C.kFSEventStreamEventFlagUserDropped | C.kFSEventStreamEventFlagKernelDropped | C.kFSEventStreamEventFlagEventIdsWrapped | - C.kFSEventStreamEventFlagRootChanged | C.kFSEventStreamEventFlagMount | C.kFSEventStreamEventFlagUnmount diff --git a/internal/dirent/watch_test.go b/internal/dirent/watch_test.go index 2d238ecfd..deefff9aa 100644 --- a/internal/dirent/watch_test.go +++ b/internal/dirent/watch_test.go @@ -183,3 +183,41 @@ func TestProbeLeavesNothing(t *testing.T) { t.Fatalf("the probe's folder holds %d entries after its waits", len(left)) } } + +// A root replaced whole, a tree renamed in under its name, is heard of +// nowhere under the root: the next Watch sees it is another folder, and +// the next read lists the new tree, not the old one's listing. +func TestRootReplacedWholeIsReadAgain(t *testing.T) { + root, day, _ := dayFolder(t) + if !quiet(t, root, day) { + t.Fatal("an unchanged watched folder was read again") + } + other := filepath.Join(t.TempDir(), "other") + rel, err := filepath.Rel(root, day) + if err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Join(other, rel), 0o755); err != nil { + t.Fatal(err) + } + b := filepath.Join(other, rel, "rollout-b.jsonl") + if err := os.WriteFile(b, []byte("{}\n"), 0o644); err != nil { + t.Fatal(err) + } + aged(t, b) + if err := os.Rename(root, root+".old"); err != nil { + t.Fatal(err) + } + if err := os.Rename(other, root); err != nil { + t.Fatal(err) + } + if _, ok := sizeOf(t, root, day, "rollout-b.jsonl"); !ok { + t.Fatal("the new tree's file wasn't seen") + } + if _, ok := sizeOf(t, root, day, "rollout-a.jsonl"); ok { + t.Fatal("the old tree's file was still listed") + } + if !quiet(t, root, day) { + t.Fatal("the new root's folder was read on every Stats after it settled") + } +}