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() { func (d *Downloader) cancel() {
// Close the current cancel channel // Close the current cancel channel
d.cancelLock.Lock() d.cancelLock.Lock()
defer d.cancelLock.Unlock()
if d.cancelCh != nil { if d.cancelCh != nil {
select { select {
case <-d.cancelCh: case <-d.cancelCh:
@ -565,7 +566,6 @@ func (d *Downloader) cancel() {
close(d.cancelCh) close(d.cancelCh)
} }
} }
d.cancelLock.Unlock()
} }
// Cancel aborts all of the operations and waits for all download goroutines to // 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. // Stop blocks until all current deliveries have finished.
func (mux *TypeMux) Stop() { func (mux *TypeMux) Stop() {
mux.mutex.Lock() mux.mutex.Lock()
defer mux.mutex.Unlock()
for _, subs := range mux.subm { for _, subs := range mux.subm {
for _, sub := range subs { for _, sub := range subs {
sub.closewait() sub.closewait()
@ -111,11 +112,11 @@ func (mux *TypeMux) Stop() {
} }
mux.subm = nil mux.subm = nil
mux.stopped = true mux.stopped = true
mux.mutex.Unlock()
} }
func (mux *TypeMux) del(s *TypeMuxSubscription) { func (mux *TypeMux) del(s *TypeMuxSubscription) {
mux.mutex.Lock() mux.mutex.Lock()
defer mux.mutex.Unlock()
for typ, subs := range mux.subm { for typ, subs := range mux.subm {
if pos := find(subs, s); pos >= 0 { if pos := find(subs, s); pos >= 0 {
if len(subs) == 1 { 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 { func find(slice []*TypeMuxSubscription, item *TypeMuxSubscription) int {
@ -196,9 +196,9 @@ func (s *TypeMuxSubscription) closewait() {
s.closed = true s.closed = true
s.postMu.Lock() s.postMu.Lock()
defer s.postMu.Unlock()
close(s.postC) close(s.postC)
s.postC = nil s.postC = nil
s.postMu.Unlock()
} }
func (s *TypeMuxSubscription) deliver(event *TypeMuxEvent) { func (s *TypeMuxSubscription) deliver(event *TypeMuxEvent) {

View file

@ -340,11 +340,11 @@ func (whisper *Whisper) getPeers() []*Peer {
arr := make([]*Peer, len(whisper.peers)) arr := make([]*Peer, len(whisper.peers))
i := 0 i := 0
whisper.peerMu.Lock() whisper.peerMu.Lock()
defer whisper.peerMu.Unlock()
for p := range whisper.peers { for p := range whisper.peers {
arr[i] = p arr[i] = p
i++ i++
} }
whisper.peerMu.Unlock()
return arr return arr
} }