Skip to content

Fix GoRoutine leak in authclient.Connect#26119

Merged
strideynet merged 3 commits intomasterfrom
strideynet/fix-authclient-goroutine-leak
May 12, 2023
Merged

Fix GoRoutine leak in authclient.Connect#26119
strideynet merged 3 commits intomasterfrom
strideynet/fix-authclient-goroutine-leak

Conversation

@strideynet
Copy link
Copy Markdown
Contributor

@strideynet strideynet commented May 12, 2023

authclient.Connect would always leak a single goroutine when connecting directly to the auth server failed. In applications that make many clients, such as tbot, this becomes a significant leak over time.

Here's a profile from before the fix for tbot: https://gist.github.com/strideynet/0712ed070cca2fac9d3c26c3fce5a341

After the fix, the number of goRoutines waiting at google.golang.org/grpc.(*ccBalancerWrapper).watcher+0x72 remains at 1, rather than slowly climbing.

Closes #26085

Copy link
Copy Markdown
Contributor

@smallinsky smallinsky left a comment

Choose a reason for hiding this comment

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

nice finding. LGTM.

Comment thread lib/auth/authclient/authclient.go Outdated
Comment on lines +81 to +135
// If it fails, we now want to try tunneling to the auth server through a
//proxy.
directDialErr := trace.Wrap(err, "failed direct dial to auth server: %v", err)
if cfg.SSH == nil {
// No identity file was provided, don't try dialing via a reverse
// tunnel on the proxy.
return nil, trace.Wrap(directDialErr)
}

// If direct dial failed, we may have a proxy address in
// cfg.AuthServers. Try connecting to the reverse tunnel
// endpoint and make a client over that.
//
// TODO(nic): this logic should be implemented once and reused in IoT
// nodes.

resolver := reversetunnel.WebClientResolver(&webclient.Config{
Context: ctx,
ProxyAddr: cfg.AuthServers[0].String(),
Insecure: cfg.TLS.InsecureSkipVerify,
Timeout: cfg.DialTimeout,
})

resolver, err = reversetunnel.CachingResolver(ctx, resolver, nil /* clock */)
if err != nil {
directDialErr := trace.Wrap(err, "failed direct dial to auth server: %v", err)
if cfg.SSH == nil {
// No identity file was provided, don't try dialing via a reverse
// tunnel on the proxy.
return nil, trace.Wrap(directDialErr)
}

// If direct dial failed, we may have a proxy address in
// cfg.AuthServers. Try connecting to the reverse tunnel
// endpoint and make a client over that.
//
// TODO(nic): this logic should be implemented once and reused in IoT
// nodes.

resolver := reversetunnel.WebClientResolver(&webclient.Config{
Context: ctx,
ProxyAddr: cfg.AuthServers[0].String(),
Insecure: cfg.TLS.InsecureSkipVerify,
Timeout: cfg.DialTimeout,
})

resolver, err = reversetunnel.CachingResolver(ctx, resolver, nil /* clock */)
if err != nil {
return nil, trace.Wrap(err)
}

// reversetunnel.TunnelAuthDialer will take care of creating a net.Conn
// within an SSH tunnel.
dialer, err := reversetunnel.NewTunnelAuthDialer(reversetunnel.TunnelAuthDialerConfig{
Resolver: resolver,
ClientConfig: cfg.SSH,
Log: cfg.Log,
InsecureSkipTLSVerify: cfg.TLS.InsecureSkipVerify,
ClusterCAs: cfg.TLS.RootCAs,
})
if err != nil {
return nil, trace.Wrap(err)
}
client, err = auth.NewClient(apiclient.Config{
Dialer: dialer,
Credentials: []apiclient.Credentials{
apiclient.LoadTLS(cfg.TLS),
},
})
if err != nil {
tunnelClientErr := trace.Wrap(err, "failed dial to auth server through reverse tunnel: %v", err)
return nil, trace.NewAggregate(directDialErr, tunnelClientErr)
}
// Check connectivity by calling something on the client.
if _, err := client.GetClusterName(); err != nil {
tunnelClientErr := trace.Wrap(err, "failed dial to auth server through reverse tunnel: %v", err)
return nil, trace.NewAggregate(directDialErr, tunnelClientErr)
}
return nil, trace.Wrap(err)
}

// reversetunnel.TunnelAuthDialer will take care of creating a net.Conn
// within an SSH tunnel.
dialer, err := reversetunnel.NewTunnelAuthDialer(reversetunnel.TunnelAuthDialerConfig{
Resolver: resolver,
ClientConfig: cfg.SSH,
Log: cfg.Log,
InsecureSkipTLSVerify: cfg.TLS.InsecureSkipVerify,
ClusterCAs: cfg.TLS.RootCAs,
})
if err != nil {
return nil, trace.Wrap(err)
}
tunnelClient, err := auth.NewClient(apiclient.Config{
Dialer: dialer,
Credentials: []apiclient.Credentials{
apiclient.LoadTLS(cfg.TLS),
},
})
if err != nil {
tunnelClientErr := trace.Wrap(err, "failed dial to auth server through reverse tunnel: %v", err)
return nil, trace.NewAggregate(directDialErr, tunnelClientErr)
}
// Check connectivity by calling something on the client.
if _, err := tunnelClient.GetClusterName(); err != nil {
_ = tunnelClient.Close() // This client didn't work for us, so we close it.
tunnelClientErr := trace.Wrap(err, "failed dial to auth server through reverse tunnel: %v", err)
return nil, trace.NewAggregate(directDialErr, tunnelClientErr)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Could we move this code to a sperate function to improve readability ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah that seems like a good idea !

@public-teleport-github-review-bot public-teleport-github-review-bot Bot removed the request for review from camscale May 12, 2023 12:15
@strideynet strideynet added this pull request to the merge queue May 12, 2023
Merged via the queue into master with commit 2e78fbb May 12, 2023
@strideynet strideynet deleted the strideynet/fix-authclient-goroutine-leak branch May 12, 2023 13:01
@public-teleport-github-review-bot
Copy link
Copy Markdown

@strideynet See the table below for backport results.

Branch Result
branch/v11 Failed
branch/v12 Failed
branch/v13 Create PR

strideynet added a commit that referenced this pull request May 12, 2023
* Fix goroutine leak in auth client connection

* Explicitly ignore .Close error

* Break each connection method into its own function for clarity
strideynet added a commit that referenced this pull request May 12, 2023
* Fix goroutine leak in auth client connection

* Explicitly ignore .Close error

* Break each connection method into its own function for clarity
strideynet added a commit that referenced this pull request May 12, 2023
* Fix goroutine leak in auth client connection

* Explicitly ignore .Close error

* Break each connection method into its own function for clarity
strideynet added a commit that referenced this pull request May 15, 2023
* Fix goroutine leak in auth client connection

* Explicitly ignore .Close error

* Break each connection method into its own function for clarity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Machine ID: tbot leaks memory

3 participants