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

Fix nil peer scope issues #104

Merged
merged 4 commits into from
Feb 3, 2022
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 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