diff --git a/pkg/rpc/context.go b/pkg/rpc/context.go index 7e8e260561c0..639def18342a 100644 --- a/pkg/rpc/context.go +++ b/pkg/rpc/context.go @@ -450,6 +450,12 @@ type Context struct { // SetLoopbackDialer configures the loopback dialer function. func (c *Context) SetLoopbackDialer(loopbackDialFn func(context.Context) (net.Conn, error)) { + if c.ContextOptions.Knobs.NoLoopbackDialer { + // A test has decided it is opting out of the special loopback + // dialing mechanism. Obey it. We already have defined + // loopbackDialFn in that case in NewContext(). + return + } c.loopbackDialFn = loopbackDialFn } @@ -1640,7 +1646,12 @@ const ( func (rpcCtx *Context) GRPCDialOptions( ctx context.Context, target string, class ConnectionClass, ) ([]grpc.DialOption, error) { - return rpcCtx.grpcDialOptionsInternal(ctx, target, class, tcpTransport) + transport := tcpTransport + if rpcCtx.Config.AdvertiseAddr == target && !rpcCtx.ClientOnly { + // See the explanation on loopbackDialFn for an explanation about this. + transport = loopbackTransport + } + return rpcCtx.grpcDialOptionsInternal(ctx, target, class, transport) } // grpcDialOptions produces dial options suitable for connecting to the given target and class. diff --git a/pkg/server/connectivity_test.go b/pkg/server/connectivity_test.go index 2ef19630d860..f84a71d568f3 100644 --- a/pkg/server/connectivity_test.go +++ b/pkg/server/connectivity_test.go @@ -125,6 +125,17 @@ func TestClusterConnectivity(t *testing.T) { // We're going to manually control initialization in this test. NoAutoInitializeCluster: true, StoreSpecs: []base.StoreSpec{{InMemory: true}}, + Knobs: base.TestingKnobs{ + Server: &server.TestingKnobs{ + ContextTestingKnobs: rpc.ContextTestingKnobs{ + // Disable the special RPC loopback dial logic. + // This is needed because the test starts to perform + // RPC dials before the PreStart() function is called, + // and that is where the loopback dial function is defined. + NoLoopbackDialer: true, + }, + }, + }, } for i, test := range testConfigurations {