]> go.fuhry.dev Git - fsnotify.git/commitdiff
[bugfix] close handle when remWatch open in getIno (#288)
authorjie <pcfunjie@gmail.com>
Sun, 31 Jul 2022 20:42:51 +0000 (04:42 +0800)
committerGitHub <noreply@github.com>
Sun, 31 Jul 2022 20:42:51 +0000 (22:42 +0200)
* [bugfix] close handle when remWatch open in getIno

* Use new tests

Note: the tests pass even without the syscall.CloseHandle()

Co-authored-by: jie <jie@tt.com>
Co-authored-by: Martin Tournoij <martin@arp242.net>
integration_test.go
windows.go

index 28b846c677d77b6490a9a3ef0412d9ae82f2ff15..fff60748613b7e6d43d65fcea9657742fe2cc238 100644 (file)
@@ -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()
index c88990480a9fdd057b596ef46d268e42d3eab4e0..e3684dfd91297184b00d66ed7c7f9a38f5109d54 100644 (file)
@@ -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)
        }