From 470ef87dd5cf672a52a6d86797ae06c4e0fef8f9 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 13 Mar 2018 13:31:26 +0100 Subject: [PATCH] vendor: Don't panic in FS watcher on old FreeBSD (fixes #4806) (#4809) This adds a recover step to the notify package to avoid the panic. We should get something like this upstreamed. --- vendor/github.com/Zillode/notify/notify.go | 35 +++++++++++++++++++++- vendor/manifest | 16 +++++----- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/vendor/github.com/Zillode/notify/notify.go b/vendor/github.com/Zillode/notify/notify.go index 293843ca..31da71fb 100644 --- a/vendor/github.com/Zillode/notify/notify.go +++ b/vendor/github.com/Zillode/notify/notify.go @@ -19,7 +19,31 @@ package notify -var defaultTree = newTree() +import "fmt" + +var defaultTree tree // lazy init + +func lazyInitDefaultTree() (err error) { + if defaultTree != nil { + // already initialized + return nil + } + + defer func() { + // newTree might panic. Patch it up. + if rec := recover(); rec != nil { + switch rec := rec.(type) { + case error: + err = rec + default: + err = fmt.Errorf("init default tree: %v", rec) + } + } + }() + + defaultTree = newTree() + return nil +} // Watch sets up a watchpoint on path listening for events given by the events // argument. @@ -61,6 +85,9 @@ var defaultTree = newTree() // e.g. use persistent paths like %userprofile% or watch additionally parent // directory of a recursive watchpoint in order to receive delete events for it. func Watch(path string, c chan<- EventInfo, events ...Event) error { + if err := lazyInitDefaultTree(); err != nil { + return err + } return defaultTree.Watch(path, c, nil, events...) } @@ -70,6 +97,9 @@ func Watch(path string, c chan<- EventInfo, events ...Event) error { // file or directory should not be watched. func WatchWithFilter(path string, c chan<- EventInfo, doNotWatch func(string) bool, events ...Event) error { + if err := lazyInitDefaultTree(); err != nil { + return err + } return defaultTree.Watch(path, c, doNotWatch, events...) } @@ -79,5 +109,8 @@ func WatchWithFilter(path string, c chan<- EventInfo, // Stop does not close c. When Stop returns, it is guaranteed that c will // receive no more signals. func Stop(c chan<- EventInfo) { + if defaultTree == nil { + return + } defaultTree.Stop(c) } diff --git a/vendor/manifest b/vendor/manifest index efdf5300..7aefb7c8 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -41,6 +41,14 @@ "branch": "master", "notests": true }, + { + "importpath": "github.com/Zillode/notify", + "repository": "https://github.com/calmh/notify", + "vcs": "git", + "revision": "53dd6873a851fc377c87d82f994b1fecdf25aadb", + "branch": "nopanic", + "notests": true + }, { "importpath": "github.com/a8m/mark", "repository": "https://github.com/a8m/mark", @@ -516,14 +524,6 @@ "path": "/qr", "notests": true }, - { - "importpath": "github.com/Zillode/notify", - "repository": "https://github.com/Zillode/notify", - "vcs": "git", - "revision": "a8abcfb1ce88ee8d79a300ed65d94b8fb616ddb3", - "branch": "master", - "notests": true - }, { "importpath": "golang.org/x/crypto/bcrypt", "repository": "https://go.googlesource.com/crypto",