Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

fix: avoid taking accessing the peerQueues without taking the lock #412

Merged
merged 2 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 1 addition & 4 deletions internal/peermanager/peermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (pm *PeerManager) getOrCreate(p peer.ID) PeerQueue {

// RegisterSession tells the PeerManager that the given session is interested
// in events about the given peer.
func (pm *PeerManager) RegisterSession(p peer.ID, s Session) bool {
func (pm *PeerManager) RegisterSession(p peer.ID, s Session) {
pm.psLk.Lock()
defer pm.psLk.Unlock()

Expand All @@ -210,9 +210,6 @@ func (pm *PeerManager) RegisterSession(p peer.ID, s Session) bool {
pm.peerSessions[p] = make(map[uint64]struct{})
}
pm.peerSessions[p][s.ID()] = struct{}{}

_, ok := pm.peerQueues[p]
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to take the pm.pqLk before accessing this.

return ok
}

// UnregisterSession tells the PeerManager that the given session is no longer
Expand Down
2 changes: 1 addition & 1 deletion internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
type PeerManager interface {
// RegisterSession tells the PeerManager that the session is interested
// in a peer's connection state
RegisterSession(peer.ID, bspm.Session) bool
RegisterSession(peer.ID, bspm.Session)
// UnregisterSession tells the PeerManager that the session is no longer
// interested in a peer's connection state
UnregisterSession(uint64)
Expand Down
4 changes: 1 addition & 3 deletions internal/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ func newFakePeerManager() *fakePeerManager {
}
}

func (pm *fakePeerManager) RegisterSession(peer.ID, bspm.Session) bool {
return true
}
func (pm *fakePeerManager) RegisterSession(peer.ID, bspm.Session) {}
func (pm *fakePeerManager) UnregisterSession(uint64) {}
func (pm *fakePeerManager) SendWants(context.Context, peer.ID, []cid.Cid, []cid.Cid) {}
func (pm *fakePeerManager) BroadcastWantHaves(ctx context.Context, cids []cid.Cid) {
Expand Down
3 changes: 1 addition & 2 deletions internal/session/sessionwantsender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ func newMockPeerManager() *mockPeerManager {
}
}

func (pm *mockPeerManager) RegisterSession(p peer.ID, sess bspm.Session) bool {
func (pm *mockPeerManager) RegisterSession(p peer.ID, sess bspm.Session) {
pm.lk.Lock()
defer pm.lk.Unlock()

pm.peerSessions[p] = sess
return true
}

func (pm *mockPeerManager) has(p peer.ID, sid uint64) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/sessionmanager/sessionmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type fakePeerManager struct {
cancels []cid.Cid
}

func (*fakePeerManager) RegisterSession(peer.ID, bspm.Session) bool { return true }
func (*fakePeerManager) RegisterSession(peer.ID, bspm.Session) {}
func (*fakePeerManager) UnregisterSession(uint64) {}
func (*fakePeerManager) SendWants(context.Context, peer.ID, []cid.Cid, []cid.Cid) {}
func (*fakePeerManager) BroadcastWantHaves(context.Context, []cid.Cid) {}
Expand Down