Skip to content

Commit 284dc00

Browse files
authored
Merge pull request #1735 from CortexFoundation/dev
suppress fsnotify error if keydir not exists
2 parents 5d9d513 + 301fa9e commit 284dc00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3189
-4830
lines changed

accounts/keystore/watch.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,31 @@
2020
package keystore
2121

2222
import (
23+
"os"
2324
"time"
2425

2526
"github.com/CortexFoundation/CortexTheseus/log"
26-
"github.com/rjeczalik/notify"
27+
"github.com/fsnotify/fsnotify"
2728
)
2829

2930
type watcher struct {
3031
ac *accountCache
31-
starting bool
32-
running bool
33-
ev chan notify.EventInfo
32+
running bool // set to true when runloop begins
33+
runEnded bool // set to true when runloop ends
34+
starting bool // set to true prior to runloop starting
3435
quit chan struct{}
3536
}
3637

3738
func newWatcher(ac *accountCache) *watcher {
3839
return &watcher{
3940
ac: ac,
40-
ev: make(chan notify.EventInfo, 10),
4141
quit: make(chan struct{}),
4242
}
4343
}
4444

45+
// enabled returns false on systems not supported.
46+
func (*watcher) enabled() bool { return true }
47+
4548
// starts the watcher loop in the background.
4649
// Start a watcher in the background if that's not already in progress.
4750
// The caller must hold w.ac.mu.
@@ -62,16 +65,26 @@ func (w *watcher) loop() {
6265
w.ac.mu.Lock()
6366
w.running = false
6467
w.starting = false
68+
w.runEnded = true
6569
w.ac.mu.Unlock()
6670
}()
6771
logger := log.New("path", w.ac.keydir)
6872

69-
if err := notify.Watch(w.ac.keydir, w.ev, notify.All); err != nil {
70-
logger.Trace("Failed to watch keystore folder", "err", err)
73+
// Create new watcher.
74+
watcher, err := fsnotify.NewWatcher()
75+
if err != nil {
76+
log.Error("Failed to start filesystem watcher", "err", err)
77+
return
78+
}
79+
defer watcher.Close()
80+
if err := watcher.Add(w.ac.keydir); err != nil {
81+
if !os.IsNotExist(err) {
82+
logger.Warn("Failed to watch keystore folder", "err", err)
83+
}
7184
return
7285
}
73-
defer notify.Stop(w.ev)
74-
logger.Trace("Started watching keystore folder")
86+
87+
logger.Trace("Started watching keystore folder", "folder", w.ac.keydir)
7588
defer logger.Trace("Stopped watching keystore folder")
7689

7790
w.ac.mu.Lock()
@@ -95,12 +108,24 @@ func (w *watcher) loop() {
95108
select {
96109
case <-w.quit:
97110
return
98-
case <-w.ev:
111+
case _, ok := <-watcher.Events:
112+
if !ok {
113+
return
114+
}
99115
// Trigger the scan (with delay), if not already triggered
100116
if !rescanTriggered {
101117
debounce.Reset(debounceDuration)
102118
rescanTriggered = true
103119
}
120+
// The fsnotify library does provide more granular event-info, it
121+
// would be possible to refresh individual affected files instead
122+
// of scheduling a full rescan. For most cases though, the
123+
// full rescan is quick and obviously simplest.
124+
case err, ok := <-watcher.Errors:
125+
if !ok {
126+
return
127+
}
128+
log.Info("Filsystem watcher error", "err", err)
104129
case <-debounce.C:
105130
w.ac.scanAccounts()
106131
rescanTriggered = false

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/fjl/gencodec v0.0.0-20230517082657-f9840df7b83e
2929
github.com/fjl/memsize v0.0.1
3030
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
31+
github.com/fsnotify/fsnotify v1.6.0
3132
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08
3233
github.com/gballet/go-verkle v0.0.0-20230620072649-dd771ce9a3b3
3334
github.com/go-stack/stack v1.8.1
@@ -53,7 +54,6 @@ require (
5354
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
5455
github.com/olekukonko/tablewriter v0.0.5
5556
github.com/peterh/liner v1.2.2
56-
github.com/rjeczalik/notify v0.9.3
5757
github.com/rs/cors v1.10.0
5858
github.com/shirou/gopsutil v3.21.11+incompatible
5959
github.com/status-im/keycard-go v0.3.0

go.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,8 +1101,6 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
11011101
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
11021102
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
11031103
github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM=
1104-
github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY=
1105-
github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
11061104
github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481/go.mod h1:C9WhFzY47SzYBIvzFqSvHIR6ROgDo4TtdTuRaOMjF/s=
11071105
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY=
11081106
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
@@ -1567,6 +1565,7 @@ golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBc
15671565
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15681566
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15691567
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1568+
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15701569
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15711570
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15721571
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

vendor/github.com/fsnotify/fsnotify/.editorconfig

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/fsnotify/fsnotify/.gitattributes

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/fsnotify/fsnotify/.gitignore

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/fsnotify/fsnotify/.mailmap

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)