Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

channelz: cleanup channel registration if Dial fails #2733

Merged
merged 2 commits into from
Apr 2, 2019
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
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work if there's a timeout set (cc.dopts.timeout > 0) ? In that case, the deferred actions will happen in the reverse order with this PR, so the auto-cancel() of the replaced ctx (with a timeout) happens before the check whether ctx is Done(), which means a cancellation error will always be returned.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right!
I filed #2736 to track. Will have a solution shortly!
Thanks for noting and mentioning this!!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got a fix & testcase on its way.

}

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