#774·fsnotify

inotify: 在不存在任何事件或错误的情况下,在卸载时丢弃监视器

作者: twz123创建于 2026年9月4日更新于 2026年9月4日
标签bug
go.mod
module repro
go 1.26
require(
        GitHub.com/fsnotify/fsnotify v1.10.2-0.20260511064106-20b1e15ef3c7
        golang.org/x/sys v0.35.0
)
</details>

<details>
<summary>main.go</summary>

```go
package main
import (
	"fmt"
	"os"
	"path/filepath"
	"time"
	"GitHub.com/fsnotify/fsnotify"
	"golang.org/x/sys/unix"
)
func main() {
	base, _ := os.MkdirTemp("", "fsnotify-unmount-")
	defer os.RemoveAll(base)
	mnt := filepath.Join(base, "mnt")
	os.Mkdir(mnt, 0o755)
	if err := unix.Mount("none", mnt, "tmpfs", 0, ""); err != nil {
		fmt.Println("mount:", err, "(run as root or under `unshare -rm`)")
		os.Exit(2)
	}
	w, _ := fsnotify.NewWatcher()
	defer w.Close()
	if err := w.Add(mnt); err != nil {
		panic(err)
	}
	fmt.Printf("WatchList before unmount: %q\n", w.WatchList())
	os.WriteFile(filepath.Join(mnt, "f"), []byte("x"), 0o644)
	drain(w, "sanity", 300*time.Millisecond)
	if err := unix.Unmount(mnt, 0); err != nil {
		panic(err)
	}
…

内容来源: fsnotify/fsnotify