Skip to content

Commit

Permalink
channelz: cleanup channel registration if Dial fails (#2733)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyuxuan authored Apr 2, 2019
1 parent d389f9f commit 955eb8a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
26 changes: 13 additions & 13 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
opt.apply(&cc.dopts)
}

defer func() {
select {
case <-ctx.Done():
conn, err = nil, ctx.Err()
default:
}

if err != nil {
cc.Close()
}
}()

if channelz.IsOn() {
if cc.dopts.channelzParentID != 0 {
cc.channelzID = channelz.RegisterChannel(&channelzChannel{cc}, cc.dopts.channelzParentID, target)
Expand Down Expand Up @@ -196,18 +208,6 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
defer cancel()
}

defer func() {
select {
case <-ctx.Done():
conn, err = nil, ctx.Err()
default:
}

if err != nil {
cc.Close()
}
}()

scSet := false
if cc.dopts.scChan != nil {
// Try to get an initial service config.
Expand Down Expand Up @@ -820,7 +820,7 @@ func (cc *ClientConn) Close() error {
}
channelz.AddTraceEvent(cc.channelzID, ted)
// TraceEvent needs to be called before RemoveEntry, as TraceEvent may add trace reference to
// the entity beng deleted, and thus prevent it from being deleted right away.
// the entity being deleted, and thus prevent it from being deleted right away.
channelz.RemoveEntry(cc.channelzID)
}
return nil
Expand Down
12 changes: 12 additions & 0 deletions test/channelz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ func (s) TestCZTopChannelRegistrationAndDeletion(t *testing.T) {
}
}

func (s) TestCZTopChannelRegistrationAndDeletionWhenDialFail(t *testing.T) {
channelz.NewChannelzStorage()
// Make dial fails (due to no transport security specified)
_, err := grpc.Dial("fake.addr")
if err == nil {
t.Fatal("expecting dial to fail")
}
if tcs, end := channelz.GetTopChannels(0, 0); tcs != nil || !end {
t.Fatalf("GetTopChannels(0, 0) = %v, %v, want <nil>, true", tcs, end)
}
}

func (s) TestCZNestedChannelRegistrationAndDeletion(t *testing.T) {
channelz.NewChannelzStorage()
e := tcpClearRREnv
Expand Down

0 comments on commit 955eb8a

Please sign in to comment.