Fix GoRoutine leak in authclient.Connect#26119
Merged
strideynet merged 3 commits intomasterfrom May 12, 2023
Merged
Conversation
smallinsky
approved these changes
May 12, 2023
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) |
Contributor
There was a problem hiding this comment.
nit: Could we move this code to a sperate function to improve readability ?
Contributor
Author
There was a problem hiding this comment.
Yeah that seems like a good idea !
tigrato
approved these changes
May 12, 2023
|
@strideynet See the table below for backport results.
|
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
authclient.Connectwould always leak a single goroutine when connecting directly to the auth server failed. In applications that make many clients, such astbot, this becomes a significant leak over time.Here's a profile from before the fix for
tbot: https://gist.github.com/strideynet/0712ed070cca2fac9d3c26c3fce5a341After the fix, the number of goRoutines waiting at
google.golang.org/grpc.(*ccBalancerWrapper).watcher+0x72remains at 1, rather than slowly climbing.Closes #26085