Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #104 from libp2p/fix/nil-peer-scope
Browse files Browse the repository at this point in the history
Fix nil peer scope issues
  • Loading branch information
vyzo authored Feb 3, 2022
2 parents a3f3cf4 + e0dde75 commit 51bc161
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ func TestListenerResourceManagement(t *testing.T) {
connScope := mocknetwork.NewMockConnManagementScope(ctrl)
gomock.InOrder(
rcmgr.EXPECT().OpenConnection(network.DirInbound, true).Return(connScope, nil),
connScope.EXPECT().PeerScope(),
connScope.EXPECT().SetPeer(id),
connScope.EXPECT().PeerScope(),
)
Expand Down
6 changes: 3 additions & 3 deletions upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ func (u *upgrader) upgrade(ctx context.Context, t transport.Transport, maconn ma
return nil, fmt.Errorf("gater rejected connection with peer %s and addr %s with direction %d",
sconn.RemotePeer().Pretty(), maconn.RemoteMultiaddr(), dir)
}
// Only call SetPeer if we didn't know the peer ID in advance.
// Otherwise, the caller will already have called it before calling Upgrade.
if p == "" {
// Only call SetPeer if it hasn't already been set -- this can happen when we don't know
// the peer in advance and in some bug scenarios.
if connScope.PeerScope() == nil {
if err := connScope.SetPeer(sconn.RemotePeer()); err != nil {
log.Debugw("resource manager blocked connection for peer", "peer", sconn.RemotePeer(), "addr", conn.RemoteAddr(), "error", err)
if err := maconn.Close(); err != nil {
Expand Down
8 changes: 7 additions & 1 deletion upgrader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ func TestOutboundResourceManagement(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
connScope := mocknetwork.NewMockConnManagementScope(ctrl)
connScope.EXPECT().PeerScope().Return(network.NullScope)
gomock.InOrder(
connScope.EXPECT().PeerScope(),
connScope.EXPECT().SetPeer(id),
connScope.EXPECT().PeerScope().Return(network.NullScope),
)
_, dialUpgrader := createUpgrader(t)
conn, err := dial(t, dialUpgrader, ln.Multiaddr(), id, connScope)
require.NoError(t, err)
Expand All @@ -171,6 +175,8 @@ func TestOutboundResourceManagement(t *testing.T) {
defer ctrl.Finish()
connScope := mocknetwork.NewMockConnManagementScope(ctrl)
gomock.InOrder(
connScope.EXPECT().PeerScope(),
connScope.EXPECT().SetPeer(id),
connScope.EXPECT().PeerScope().Return(network.NullScope),
connScope.EXPECT().Done(),
)
Expand Down

0 comments on commit 51bc161

Please sign in to comment.