Skip to content

Commit 2bc8ab8

Browse files
committed
dspinner: RecursiveKeys(): do not hang on cancellations
Per ipfs/kubo#10593, if no one is reading from the channel returned by RecursiveKeys() and the context is cancelled, streamIndex will hang indefinitely. Proposed fix is to always select when attempting to write to the `out` channel. If the context is done and there is no one to read, we can abort.
1 parent c91cc1d commit 2bc8ab8

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

pinning/pinner/dspinner/pin.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -707,19 +707,27 @@ func (p *pinner) streamIndex(ctx context.Context, index dsindex.Indexer, detaile
707707
defer p.lock.RUnlock()
708708

709709
cidSet := cid.NewSet()
710+
send := func(sp ipfspinner.StreamedPin) (ok bool) {
711+
select {
712+
case <-ctx.Done():
713+
return false
714+
case out <- sp:
715+
return true
716+
}
717+
}
710718

711719
err := index.ForEach(ctx, "", func(key, value string) bool {
712720
c, err := cid.Cast([]byte(key))
713721
if err != nil {
714-
out <- ipfspinner.StreamedPin{Err: err}
722+
send(ipfspinner.StreamedPin{Err: err})
715723
return false
716724
}
717725

718726
var pin ipfspinner.Pinned
719727
if detailed {
720728
pp, err := p.loadPin(ctx, value)
721729
if err != nil {
722-
out <- ipfspinner.StreamedPin{Err: err}
730+
send(ipfspinner.StreamedPin{Err: err})
723731
return false
724732
}
725733

@@ -731,17 +739,16 @@ func (p *pinner) streamIndex(ctx context.Context, index dsindex.Indexer, detaile
731739
}
732740

733741
if !cidSet.Has(c) {
734-
select {
735-
case <-ctx.Done():
742+
if !send(ipfspinner.StreamedPin{Pin: pin}) {
736743
return false
737-
case out <- ipfspinner.StreamedPin{Pin: pin}:
738744
}
739745
cidSet.Add(c)
740746
}
741747
return true
742748
})
743749
if err != nil {
744-
out <- ipfspinner.StreamedPin{Err: err}
750+
send(ipfspinner.StreamedPin{Err: err})
751+
return
745752
}
746753
}()
747754

0 commit comments

Comments
 (0)