OSDN Git Service

netsync: Fix crash when unsubscribe event on exit (#1543)
authoryahtoo <yahtoo.ma@gmail.com>
Fri, 4 Jan 2019 02:06:46 +0000 (10:06 +0800)
committerPaladz <yzhu101@uottawa.ca>
Fri, 4 Jan 2019 02:06:46 +0000 (10:06 +0800)
* netsync: Fix crash when unsubscribe event on exit

* Revert "Fix crash when unsubscribe event on exit"

This reverts commit 7533c2f010081d3ffd90b91887d04c52f8429ea7.

* Fix review bug

* Fix review bug

* Fix review bug

event/event.go
netsync/handle.go

index 8a1bf51..4037931 100644 (file)
@@ -37,8 +37,6 @@ type TypeMuxEvent struct {
 // called after mux is stopped will return ErrMuxClosed.
 //
 // The zero value is ready to use.
-//
-// Deprecated: use Feed
 type Dispatcher struct {
        mutex   sync.RWMutex
        subm    map[reflect.Type][]*Subscription
index 3d833c1..9bfef28 100644 (file)
@@ -350,7 +350,7 @@ func (sm *SyncManager) handleTransactionMsg(peer *peer, msg *TransactionMessage)
                return
        }
 
-       if isOrphan, err := sm.chain.ValidateTx(tx); err != nil && isOrphan == false {
+       if isOrphan, err := sm.chain.ValidateTx(tx); err != nil && !isOrphan {
                sm.peers.addBanScore(peer.ID(), 10, 0, "fail on validate tx transaction")
        }
 }
@@ -476,8 +476,8 @@ func (sm *SyncManager) Start() {
 
 //Stop stop sync manager
 func (sm *SyncManager) Stop() {
-       sm.minedBlockSub.Unsubscribe()
        close(sm.quitSync)
+       sm.minedBlockSub.Unsubscribe()
        sm.sw.Stop()
 }
 
@@ -517,7 +517,12 @@ func initDiscover(config *cfg.Config, priv *crypto.PrivKeyEd25519, port uint16)
 func (sm *SyncManager) minedBroadcastLoop() {
        for {
                select {
-               case obj := <-sm.minedBlockSub.Chan():
+               case obj, ok := <-sm.minedBlockSub.Chan():
+                       if !ok {
+                               log.WithFields(log.Fields{"module": logModule}).Warning("mined block subscription channel closed")
+                               return
+                       }
+
                        ev, ok := obj.Data.(event.NewMinedBlockEvent)
                        if !ok {
                                log.WithFields(log.Fields{"module": logModule}).Error("event type error")