mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
fix: fsnotify watcher panics when closing (#3399)
This commit is contained in:
@ -106,11 +106,11 @@ func (w *Watcher) addWithCallbackFunc(name, path string, callbackFunc func(event
|
||||
|
||||
// Close closes the watcher.
|
||||
func (w *Watcher) Close() {
|
||||
w.events.Close()
|
||||
close(w.closeChan)
|
||||
if err := w.watcher.Close(); err != nil {
|
||||
intlog.Errorf(context.TODO(), `%+v`, err)
|
||||
}
|
||||
close(w.closeChan)
|
||||
w.events.Close()
|
||||
}
|
||||
|
||||
// Remove removes monitor and all callbacks associated with the `path` recursively.
|
||||
|
||||
@ -8,6 +8,7 @@ package gfsnotify
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
|
||||
@ -25,7 +26,10 @@ func (w *Watcher) watchLoop() {
|
||||
return
|
||||
|
||||
// Event listening.
|
||||
case ev := <-w.watcher.Events:
|
||||
case ev, ok := <-w.watcher.Events:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// Filter the repeated event in custom duration.
|
||||
_, err := w.cache.SetIfNotExist(
|
||||
context.Background(),
|
||||
|
||||
@ -218,3 +218,23 @@ func TestWatcher_WatchFolderWithoutRecursively(t *testing.T) {
|
||||
t.Assert(array.Len(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWatcher_WatchClose(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
err error
|
||||
dirPath = gfile.Temp(gtime.TimestampNanoStr())
|
||||
watcher *gfsnotify.Watcher
|
||||
)
|
||||
|
||||
err = gfile.Mkdir(dirPath)
|
||||
t.AssertNil(err)
|
||||
|
||||
watcher, err = gfsnotify.New()
|
||||
t.AssertNil(err)
|
||||
t.AssertNE(watcher, nil)
|
||||
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
watcher.Close()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user