Commit ff038ecf authored by Jeromy's avatar Jeromy

fix random bitswap hangs

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent a06e4b5d
...@@ -56,6 +56,8 @@ type msgQueue struct { ...@@ -56,6 +56,8 @@ type msgQueue struct {
out bsmsg.BitSwapMessage out bsmsg.BitSwapMessage
network bsnet.BitSwapNetwork network bsnet.BitSwapNetwork
refcnt int
work chan struct{} work chan struct{}
done chan struct{} done chan struct{}
} }
...@@ -101,13 +103,13 @@ func (pm *WantManager) SendBlock(ctx context.Context, env *engine.Envelope) { ...@@ -101,13 +103,13 @@ func (pm *WantManager) SendBlock(ctx context.Context, env *engine.Envelope) {
} }
func (pm *WantManager) startPeerHandler(p peer.ID) *msgQueue { func (pm *WantManager) startPeerHandler(p peer.ID) *msgQueue {
_, ok := pm.peers[p] mq, ok := pm.peers[p]
if ok { if ok {
// TODO: log an error? mq.refcnt++
return nil return nil
} }
mq := pm.newMsgQueue(p) mq = pm.newMsgQueue(p)
// new peer, we will want to give them our full wantlist // new peer, we will want to give them our full wantlist
fullwantlist := bsmsg.New(true) fullwantlist := bsmsg.New(true)
...@@ -129,6 +131,11 @@ func (pm *WantManager) stopPeerHandler(p peer.ID) { ...@@ -129,6 +131,11 @@ func (pm *WantManager) stopPeerHandler(p peer.ID) {
return return
} }
pq.refcnt--
if pq.refcnt > 0 {
return
}
close(pq.done) close(pq.done)
delete(pm.peers, p) delete(pm.peers, p)
} }
...@@ -247,6 +254,7 @@ func (wm *WantManager) newMsgQueue(p peer.ID) *msgQueue { ...@@ -247,6 +254,7 @@ func (wm *WantManager) newMsgQueue(p peer.ID) *msgQueue {
mq.work = make(chan struct{}, 1) mq.work = make(chan struct{}, 1)
mq.network = wm.network mq.network = wm.network
mq.p = p mq.p = p
mq.refcnt = 1
return mq return mq
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment