Skip to content

bitswap: clean up ledgers when disconnecting #3437

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

Merged
merged 2 commits into from
May 20, 2017
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
1 change: 1 addition & 0 deletions exchange/bitswap/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
// Connected/Disconnected warns bitswap about peer connections
func (bs *Bitswap) PeerConnected(p peer.ID) {
bs.wm.Connected(p)
bs.engine.PeerConnected(p)
}

// Connected/Disconnected warns bitswap about peer connections
Expand Down
26 changes: 25 additions & 1 deletion exchange/bitswap/decision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,32 @@ func (e *Engine) MessageSent(p peer.ID, m bsmsg.BitSwapMessage) error {
return nil
}

func (e *Engine) PeerConnected(p peer.ID) {
e.lock.Lock()
l, ok := e.ledgerMap[p]
if !ok {
l = newLedger(p)
e.ledgerMap[p] = l
}
l.lk.Lock()
l.ref++
l.lk.Unlock()
e.lock.Unlock()
}

func (e *Engine) PeerDisconnected(p peer.ID) {
// TODO: release ledger
e.lock.Lock()
defer e.lock.Unlock()
l, ok := e.ledgerMap[p]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are not locking l.lk here, and again we have situation with two locks. It shouts to me deadlock.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the ledger lock. Re locking concerns, these ones are well scoped. the engine lock is either always held first, or not held while taking the ledger lock. And the engine lock is never taken while holding a ledger lock.

if !ok {
return
}
l.lk.Lock()
l.ref--
if l.ref <= 0 {
delete(e.ledgerMap, p)
}
l.lk.Unlock()
}

func (e *Engine) numBytesSentTo(p peer.ID) uint64 {
Expand Down
5 changes: 5 additions & 0 deletions exchange/bitswap/decision/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func TestPeerIsAddedToPeersWhenMessageReceivedOrSent(t *testing.T) {
if !peerIsPartner(sanfrancisco.Peer, seattle.Engine) {
t.Fatal("Peer wasn't added as a Partner")
}

seattle.Engine.PeerDisconnected(sanfrancisco.Peer)
if peerIsPartner(sanfrancisco.Peer, seattle.Engine) {
t.Fatal("expected peer to be removed")
}
}

func peerIsPartner(p peer.ID, e *Engine) bool {
Expand Down
4 changes: 4 additions & 0 deletions exchange/bitswap/decision/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ type ledger struct {
// to a given peer
sentToPeer map[string]time.Time

// ref is the reference count for this ledger, its used to ensure we
// don't drop the reference to this ledger in multi-connection scenarios
ref int

lk sync.Mutex
}

Expand Down