From: jie Date: Sun, 31 Jul 2022 20:42:51 +0000 (+0800) Subject: [bugfix] close handle when remWatch open in getIno (#288) X-Git-Tag: v1.7.2~86 X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=928895c7325a60d9cbf344e516947cfc435119a7;p=fsnotify.git [bugfix] close handle when remWatch open in getIno (#288) * [bugfix] close handle when remWatch open in getIno * Use new tests Note: the tests pass even without the syscall.CloseHandle() Co-authored-by: jie Co-authored-by: Martin Tournoij --- diff --git a/integration_test.go b/integration_test.go index 28b846c..fff6074 100644 --- a/integration_test.go +++ b/integration_test.go @@ -452,6 +452,8 @@ func TestClose(t *testing.T) { }) } +// TODO: should also check internal state is correct/cleaned up; e.g. no +// left-over file descriptors or whatnot. func TestRemove(t *testing.T) { t.Run("works", func(t *testing.T) { t.Parallel() @@ -476,6 +478,24 @@ func TestRemove(t *testing.T) { } }) + t.Run("remove same dir twice", func(t *testing.T) { + tmp := t.TempDir() + + touch(t, tmp, "file") + + w := newWatcher(t) + defer w.Close() + + addWatch(t, w, tmp) + + if err := w.Remove(tmp); err != nil { + t.Fatal(err) + } + if err := w.Remove(tmp); err == nil { + t.Fatal("no error") + } + }) + // Make sure that concurrent calls to Remove() don't race. t.Run("no race", func(t *testing.T) { t.Parallel() diff --git a/windows.go b/windows.go index c889904..e3684df 100644 --- a/windows.go +++ b/windows.go @@ -327,6 +327,9 @@ func (w *Watcher) remWatch(pathname string) error { w.mu.Lock() watch := w.watches.get(ino) w.mu.Unlock() + if err := syscall.CloseHandle(ino.handle); err != nil { + w.Errors <- os.NewSyscallError("CloseHandle", err) + } if watch == nil { return fmt.Errorf("%w: %s", ErrNonExistentWatch, pathname) }