Skip to content

Commit 41785f3

Browse files
committed
Merge pull request #1356 from ipfs/fix/bitswap-leak
prevent wantmanager from leaking goroutines (and memory)
2 parents 952dc9c + e014a66 commit 41785f3

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

exchange/bitswap/wantmanager.go

+37-24
Original file line numberDiff line numberDiff line change
@@ -137,36 +137,49 @@ func (mq *msgQueue) runQueue(ctx context.Context) {
137137
for {
138138
select {
139139
case <-mq.work: // there is work to be done
140-
141-
err := mq.network.ConnectTo(ctx, mq.p)
142-
if err != nil {
143-
log.Noticef("cant connect to peer %s: %s", mq.p, err)
144-
// TODO: cant connect, what now?
145-
continue
146-
}
147-
148-
// grab outgoing message
149-
mq.outlk.Lock()
150-
wlm := mq.out
151-
if wlm == nil || wlm.Empty() {
152-
mq.outlk.Unlock()
153-
continue
154-
}
155-
mq.out = nil
156-
mq.outlk.Unlock()
157-
158-
// send wantlist updates
159-
err = mq.network.SendMessage(ctx, mq.p, wlm)
160-
if err != nil {
161-
log.Noticef("bitswap send error: %s", err)
162-
// TODO: what do we do if this fails?
163-
}
140+
mq.doWork(ctx)
164141
case <-mq.done:
165142
return
166143
}
167144
}
168145
}
169146

147+
func (mq *msgQueue) doWork(ctx context.Context) {
148+
// allow ten minutes for connections
149+
// this includes looking them up in the dht
150+
// dialing them, and handshaking
151+
conctx, cancel := context.WithTimeout(ctx, time.Minute*10)
152+
defer cancel()
153+
154+
err := mq.network.ConnectTo(conctx, mq.p)
155+
if err != nil {
156+
log.Noticef("cant connect to peer %s: %s", mq.p, err)
157+
// TODO: cant connect, what now?
158+
return
159+
}
160+
161+
// grab outgoing message
162+
mq.outlk.Lock()
163+
wlm := mq.out
164+
if wlm == nil || wlm.Empty() {
165+
mq.outlk.Unlock()
166+
return
167+
}
168+
mq.out = nil
169+
mq.outlk.Unlock()
170+
171+
sendctx, cancel := context.WithTimeout(ctx, time.Minute*5)
172+
defer cancel()
173+
174+
// send wantlist updates
175+
err = mq.network.SendMessage(sendctx, mq.p, wlm)
176+
if err != nil {
177+
log.Noticef("bitswap send error: %s", err)
178+
// TODO: what do we do if this fails?
179+
return
180+
}
181+
}
182+
170183
func (pm *WantManager) Connected(p peer.ID) {
171184
pm.connect <- p
172185
}

0 commit comments

Comments
 (0)