eth/download,event,whisper : add some defers when unlock

This commit is contained in:
ucwong 2020-04-16 15:01:21 +00:00
parent 7a63faf734
commit 38916f3c35
3 changed files with 5 additions and 5 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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
}