Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bitswap): memory leak on BlockPresenceManager #636

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ The following emojis are used to highlight certain changes:

### Fixed

- `bitswap/client` fix memory leak in BlockPresenceManager due to unlimited map growth.

### Security

## [v0.21.0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
}

func New() *BlockPresenceManager {
return &BlockPresenceManager{
presence: make(map[cid.Cid]map[peer.ID]bool),
}
return &BlockPresenceManager{}
}

// ReceiveFrom is called when a peer sends us information about which blocks
Expand All @@ -26,6 +24,10 @@
bpm.Lock()
defer bpm.Unlock()

if bpm.presence == nil {
bpm.presence = make(map[cid.Cid]map[peer.ID]bool)
}

for _, c := range haves {
bpm.updateBlockPresence(p, c, true)
}
Expand Down Expand Up @@ -75,6 +77,10 @@
bpm.RLock()
defer bpm.RUnlock()

if len(bpm.presence) == 0 {
return nil
}

var res []cid.Cid
for _, c := range ks {
if bpm.allDontHave(peers, c) {
Expand All @@ -90,6 +96,9 @@
if !cok {
return false
}
if len(ps) == 0 {
return false
}

Check warning on line 101 in bitswap/client/internal/blockpresencemanager/blockpresencemanager.go

View check run for this annotation

Codecov / codecov/patch

bitswap/client/internal/blockpresencemanager/blockpresencemanager.go#L100-L101

Added lines #L100 - L101 were not covered by tests

// Check if we explicitly know that all the given peers do not have the cid
for _, p := range peers {
Expand All @@ -108,6 +117,25 @@
for _, c := range ks {
delete(bpm.presence, c)
}
if len(bpm.presence) == 0 {
bpm.presence = nil
}
}

// RemovePeers removes the given peer from every cid key in the presence map.
func (bpm *BlockPresenceManager) RemovePeer(p peer.ID) {
bpm.Lock()
defer bpm.Unlock()

for c, pm := range bpm.presence {
delete(pm, p)
if len(pm) == 0 {
delete(bpm.presence, c)
}
}
if len(bpm.presence) == 0 {
bpm.presence = nil
}
}

// HasKey indicates whether the BlockPresenceManager is tracking the given key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ func TestBlockPresenceManager(t *testing.T) {
if bpm.PeerDoesNotHaveBlock(p, c1) {
t.Fatal(expDoesNotHaveFalseMsg)
}

bpm.ReceiveFrom(p, []cid.Cid{c0}, []cid.Cid{c1})
if !bpm.PeerHasBlock(p, c0) {
t.Fatal(expHasTrueMsg)
}
if !bpm.PeerDoesNotHaveBlock(p, c1) {
t.Fatal(expDoesNotHaveTrueMsg)
}
bpm.RemovePeer(p)
if bpm.PeerHasBlock(p, c0) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p, c0) {
t.Fatal(expDoesNotHaveFalseMsg)
}
if bpm.PeerHasBlock(p, c1) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p, c1) {
t.Fatal(expDoesNotHaveFalseMsg)
}
}

func TestAddRemoveMulti(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions bitswap/client/internal/session/sessionwantsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ func (sws *sessionWantSender) processUpdates(updates []update) []cid.Cid {
go func() {
for p := range prunePeers {
// Peer doesn't have anything we want, so remove it
sws.bpm.RemovePeer(p)
log.Infof("peer %s sent too many dont haves, removing from session %d", p, sws.ID())
sws.SignalAvailability(p, false)
}
Expand Down