From 43d937ed15a1563be7a4cb20cf367559addd47e7 Mon Sep 17 00:00:00 2001 From: Mark Tully Date: Fri, 7 Feb 2020 23:10:41 +0000 Subject: [PATCH] fix panic when file/dir is deleted sometimes when a file/dir which is being watched is deleted a panic will occur. This appears to be because the is an assumption that if an error returns true for `IsNotExist()` that it should be castable to `os.PathError`, but this evidently is not always the case, causing a panic. --- watcher.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/watcher.go b/watcher.go index 4da4dfe..c147b30 100644 --- a/watcher.go +++ b/watcher.go @@ -497,9 +497,12 @@ func (w *Watcher) retrieveFileList() map[string]os.FileInfo { if err != nil { if os.IsNotExist(err) { w.mu.Unlock() - if name == err.(*os.PathError).Path { - w.Error <- ErrWatchedFileDeleted - w.RemoveRecursive(name) + pathErr, ok := err.(*os.PathError) + if ok { + if name == pathErr.Path { + w.Error <- ErrWatchedFileDeleted + w.RemoveRecursive(name) + } } w.mu.Lock() } else { @@ -511,9 +514,12 @@ func (w *Watcher) retrieveFileList() map[string]os.FileInfo { if err != nil { if os.IsNotExist(err) { w.mu.Unlock() - if name == err.(*os.PathError).Path { - w.Error <- ErrWatchedFileDeleted - w.Remove(name) + pathErr, ok := err.(*os.PathError) + if ok { + if name == pathErr.Path { + w.Error <- ErrWatchedFileDeleted + w.Remove(name) + } } w.mu.Lock() } else {