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

Commit 51bc161

Browse files
authored
Merge pull request #104 from libp2p/fix/nil-peer-scope
Fix nil peer scope issues
2 parents a3f3cf4 + e0dde75 commit 51bc161

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

listener_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ func TestListenerResourceManagement(t *testing.T) {
363363
connScope := mocknetwork.NewMockConnManagementScope(ctrl)
364364
gomock.InOrder(
365365
rcmgr.EXPECT().OpenConnection(network.DirInbound, true).Return(connScope, nil),
366+
connScope.EXPECT().PeerScope(),
366367
connScope.EXPECT().SetPeer(id),
367368
connScope.EXPECT().PeerScope(),
368369
)

upgrader.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ func (u *upgrader) upgrade(ctx context.Context, t transport.Transport, maconn ma
157157
return nil, fmt.Errorf("gater rejected connection with peer %s and addr %s with direction %d",
158158
sconn.RemotePeer().Pretty(), maconn.RemoteMultiaddr(), dir)
159159
}
160-
// Only call SetPeer if we didn't know the peer ID in advance.
161-
// Otherwise, the caller will already have called it before calling Upgrade.
162-
if p == "" {
160+
// Only call SetPeer if it hasn't already been set -- this can happen when we don't know
161+
// the peer in advance and in some bug scenarios.
162+
if connScope.PeerScope() == nil {
163163
if err := connScope.SetPeer(sconn.RemotePeer()); err != nil {
164164
log.Debugw("resource manager blocked connection for peer", "peer", sconn.RemotePeer(), "addr", conn.RemoteAddr(), "error", err)
165165
if err := maconn.Close(); err != nil {

upgrader_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ func TestOutboundResourceManagement(t *testing.T) {
153153
ctrl := gomock.NewController(t)
154154
defer ctrl.Finish()
155155
connScope := mocknetwork.NewMockConnManagementScope(ctrl)
156-
connScope.EXPECT().PeerScope().Return(network.NullScope)
156+
gomock.InOrder(
157+
connScope.EXPECT().PeerScope(),
158+
connScope.EXPECT().SetPeer(id),
159+
connScope.EXPECT().PeerScope().Return(network.NullScope),
160+
)
157161
_, dialUpgrader := createUpgrader(t)
158162
conn, err := dial(t, dialUpgrader, ln.Multiaddr(), id, connScope)
159163
require.NoError(t, err)
@@ -171,6 +175,8 @@ func TestOutboundResourceManagement(t *testing.T) {
171175
defer ctrl.Finish()
172176
connScope := mocknetwork.NewMockConnManagementScope(ctrl)
173177
gomock.InOrder(
178+
connScope.EXPECT().PeerScope(),
179+
connScope.EXPECT().SetPeer(id),
174180
connScope.EXPECT().PeerScope().Return(network.NullScope),
175181
connScope.EXPECT().Done(),
176182
)

0 commit comments

Comments
 (0)