From 38916f3c35128a6e60cd4d8100bcb447516aa686 Mon Sep 17 00:00:00 2001 From: ucwong Date: Thu, 16 Apr 2020 15:01:21 +0000 Subject: [PATCH] eth/download,event,whisper : add some defers when unlock --- eth/downloader/downloader.go | 2 +- event/event.go | 6 +++--- whisper/whisperv6/whisper.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index f8982f696f..952a99efe4 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -557,6 +557,7 @@ func (d *Downloader) spawnSync(fetchers []func() error) error { func (d *Downloader) cancel() { // Close the current cancel channel d.cancelLock.Lock() + defer d.cancelLock.Unlock() if d.cancelCh != nil { select { case <-d.cancelCh: @@ -565,7 +566,6 @@ func (d *Downloader) cancel() { close(d.cancelCh) } } - d.cancelLock.Unlock() } // Cancel aborts all of the operations and waits for all download goroutines to diff --git a/event/event.go b/event/event.go index 4232787314..ce1b03d523 100644 --- a/event/event.go +++ b/event/event.go @@ -104,6 +104,7 @@ func (mux *TypeMux) Post(ev interface{}) error { // Stop blocks until all current deliveries have finished. func (mux *TypeMux) Stop() { mux.mutex.Lock() + defer mux.mutex.Unlock() for _, subs := range mux.subm { for _, sub := range subs { sub.closewait() @@ -111,11 +112,11 @@ func (mux *TypeMux) Stop() { } mux.subm = nil mux.stopped = true - mux.mutex.Unlock() } func (mux *TypeMux) del(s *TypeMuxSubscription) { mux.mutex.Lock() + defer mux.mutex.Unlock() for typ, subs := range mux.subm { if pos := find(subs, s); pos >= 0 { if len(subs) == 1 { @@ -125,7 +126,6 @@ func (mux *TypeMux) del(s *TypeMuxSubscription) { } } } - s.mux.mutex.Unlock() } func find(slice []*TypeMuxSubscription, item *TypeMuxSubscription) int { @@ -196,9 +196,9 @@ func (s *TypeMuxSubscription) closewait() { s.closed = true s.postMu.Lock() + defer s.postMu.Unlock() close(s.postC) s.postC = nil - s.postMu.Unlock() } func (s *TypeMuxSubscription) deliver(event *TypeMuxEvent) { diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go index 377406b360..af513888d0 100644 --- a/whisper/whisperv6/whisper.go +++ b/whisper/whisperv6/whisper.go @@ -340,11 +340,11 @@ func (whisper *Whisper) getPeers() []*Peer { arr := make([]*Peer, len(whisper.peers)) i := 0 whisper.peerMu.Lock() + defer whisper.peerMu.Unlock() for p := range whisper.peers { arr[i] = p i++ } - whisper.peerMu.Unlock() return arr }