From 928895c7325a60d9cbf344e516947cfc435119a7 Mon Sep 17 00:00:00 2001 From: jie Date: Mon, 1 Aug 2022 04:42:51 +0800 Subject: [PATCH] [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 --- integration_test.go | 20 ++++++++++++++++++++ windows.go | 3 +++ 2 files changed, 23 insertions(+) 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) } -- 2.50.1