Skip to content

Commit 955eb8a

Browse files
authored
channelz: cleanup channel registration if Dial fails (#2733)
1 parent d389f9f commit 955eb8a

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

Diff for: clientconn.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
134134
opt.apply(&cc.dopts)
135135
}
136136

137+
defer func() {
138+
select {
139+
case <-ctx.Done():
140+
conn, err = nil, ctx.Err()
141+
default:
142+
}
143+
144+
if err != nil {
145+
cc.Close()
146+
}
147+
}()
148+
137149
if channelz.IsOn() {
138150
if cc.dopts.channelzParentID != 0 {
139151
cc.channelzID = channelz.RegisterChannel(&channelzChannel{cc}, cc.dopts.channelzParentID, target)
@@ -196,18 +208,6 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
196208
defer cancel()
197209
}
198210

199-
defer func() {
200-
select {
201-
case <-ctx.Done():
202-
conn, err = nil, ctx.Err()
203-
default:
204-
}
205-
206-
if err != nil {
207-
cc.Close()
208-
}
209-
}()
210-
211211
scSet := false
212212
if cc.dopts.scChan != nil {
213213
// Try to get an initial service config.
@@ -820,7 +820,7 @@ func (cc *ClientConn) Close() error {
820820
}
821821
channelz.AddTraceEvent(cc.channelzID, ted)
822822
// TraceEvent needs to be called before RemoveEntry, as TraceEvent may add trace reference to
823-
// the entity beng deleted, and thus prevent it from being deleted right away.
823+
// the entity being deleted, and thus prevent it from being deleted right away.
824824
channelz.RemoveEntry(cc.channelzID)
825825
}
826826
return nil

Diff for: test/channelz_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ func (s) TestCZTopChannelRegistrationAndDeletion(t *testing.T) {
190190
}
191191
}
192192

193+
func (s) TestCZTopChannelRegistrationAndDeletionWhenDialFail(t *testing.T) {
194+
channelz.NewChannelzStorage()
195+
// Make dial fails (due to no transport security specified)
196+
_, err := grpc.Dial("fake.addr")
197+
if err == nil {
198+
t.Fatal("expecting dial to fail")
199+
}
200+
if tcs, end := channelz.GetTopChannels(0, 0); tcs != nil || !end {
201+
t.Fatalf("GetTopChannels(0, 0) = %v, %v, want <nil>, true", tcs, end)
202+
}
203+
}
204+
193205
func (s) TestCZNestedChannelRegistrationAndDeletion(t *testing.T) {
194206
channelz.NewChannelzStorage()
195207
e := tcpClearRREnv

0 commit comments

Comments
 (0)