dirent: watch without FSEvents' WatchRoot, which left files EBADF (#6368, #6785)

macOS CI's internal/sessions runs failed on ordinary file reads, writes,
closes and TempDir cleanup with "bad file descriptor" since 11-13, the
day after 6f3e76626 created the FSEvents stream with
kFSEventStreamCreateFlagWatchRoot. A probe (draft #6795, run
43778622224) ran 8 goroutines of plain file I/O beside a loop that
watches and removes temp roots, 45s per phase, flag on, off, on, off:

  macOS 15.8.2: on 0 + 554 EBADF, off 0 + 0 (~740k ops)
  macOS 26.7.2: on 0 + 362 EBADF, off 0 + 0 (~650k ops)

mote (macOS 27.2) saw none either way, so it was CI-only.

The stream is made without the flag. What it gave, word of a root
renamed, removed or put back, WatchSince now checks itself: each Watch
stats the root, and when it is not the folder the watch began on
(os.SameFile), that root's watch is dropped, everything is listed again,
and the watch begins anew on what is there.

Run (mote, at this tree):
- go test -race -count=5 ./internal/dirent: ok
- go test ./internal/dirent ./internal/sessions: ok
- go vet ./internal/dirent for darwin, linux, windows; gofmt -l: clean
- broken by hand: with the rootMoved check skipped,
  TestRootReplacedWholeIsReadAgain is red 3 of 3 ("the new tree's file
  wasn't seen").
Not tried: the full suite (CI runs it); EBADF's absence on main's CI
over time is the real proof, watched on #6368.
This commit is contained in:
yetone
2026-11-16 08:21:22 +08:00
parent 421d5ce000
commit 0522bf84f8
3 changed files with 86 additions and 7 deletions
+42 -3
View File
@@ -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()
+6 -4
View File
@@ -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
+38
View File
@@ -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")
}
}