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

[1.14.x] cache: fix a few minor goroutine leaks in leaf certs and the agent cache #17638

Merged
merged 1 commit into from
Jun 9, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/17636.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cache: fix a few minor goroutine leaks in leaf certs and the agent cache
```
20 changes: 13 additions & 7 deletions agent/cache-types/connect_ca_leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
"github.com/mitchellh/hashstructure"

"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/lib"

"github.com/hashicorp/consul/agent/cache"
"github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/agent/consul"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/lib"
)

// Recommended name for registration.
Expand Down Expand Up @@ -424,20 +423,25 @@ func (c *ConnectCALeaf) Fetch(opts cache.FetchOptions, req cache.Request) (cache

// Setup the timeout chan outside the loop so we don't keep bumping the timeout
// later if we loop around.
timeoutCh := time.After(opts.Timeout)
timeoutTimer := time.NewTimer(opts.Timeout)
defer timeoutTimer.Stop()

// Setup initial expiry chan. We may change this if root update occurs in the
// loop below.
expiresCh := time.After(expiresAt.Sub(now))
expiresTimer := time.NewTimer(expiresAt.Sub(now))
defer func() {
// Resolve the timer reference at defer time, so we use the latest one each time.
expiresTimer.Stop()
}()

// Current cert is valid so just wait until it expires or we time out.
for {
select {
case <-timeoutCh:
case <-timeoutTimer.C:
// We timed out the request with same cert.
return lastResultWithNewState(), nil

case <-expiresCh:
case <-expiresTimer.C:
// Cert expired or was force-expired by a root change.
return c.generateNewLeaf(reqReal, lastResultWithNewState())

Expand Down Expand Up @@ -478,7 +482,9 @@ func (c *ConnectCALeaf) Fetch(opts cache.FetchOptions, req cache.Request) (cache
// loop back around, we'll wait at most delay until generating a new cert.
if state.forceExpireAfter.Before(expiresAt) {
expiresAt = state.forceExpireAfter
expiresCh = time.After(delay)
// Stop the former one and create a new one.
expiresTimer.Stop()
expiresTimer = time.NewTimer(delay)
}
continue
}
Expand Down
7 changes: 5 additions & 2 deletions agent/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const (
// rate limiter settings.

// DefaultEntryFetchRate is the default rate at which cache entries can
// be fetch. This defaults to not being unlimited
// be fetch. This defaults to not being limited
DefaultEntryFetchRate = rate.Inf

// DefaultEntryFetchMaxBurst is the number of cache entry fetches that can
Expand Down Expand Up @@ -533,7 +533,10 @@ RETRY_GET:

// Set our timeout channel if we must
if r.Info.Timeout > 0 && timeoutCh == nil {
timeoutCh = time.After(r.Info.Timeout)
timeoutTimer := time.NewTimer(r.Info.Timeout)
defer timeoutTimer.Stop()

timeoutCh = timeoutTimer.C
}

// At this point, we know we either don't have a value at all or the
Expand Down