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

Backport of VAULT-4240 time.After() in a select statement can lead to memory leak into release/1.10.x #14826

Merged
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
3 changes: 3 additions & 0 deletions changelog/14814.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
core: time.After() used in a select statement can lead to memory leak
```
7 changes: 6 additions & 1 deletion helper/fairshare/jobmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ func (j *JobManager) assignWork() {
j.wg.Add(1)

go func() {
// ticker is used to prevent memory leak of using time.After in
// for - select pattern.
ticker := time.NewTicker(50 * time.Millisecond)
defer ticker.Stop()
for {
for {
// assign work while there are jobs to distribute
Expand All @@ -291,13 +295,14 @@ func (j *JobManager) assignWork() {
}
}

ticker.Reset(50 * time.Millisecond)
select {
case <-j.quit:
j.wg.Done()
return
case <-j.newWork:
// listen for wake-up when an empty job manager has been given work
case <-time.After(50 * time.Millisecond):
case <-ticker.C:
// periodically check if new workers can be assigned. with the
// fairsharing worker distribution it can be the case that there
// is work waiting, but no queues are eligible for another worker
Expand Down
7 changes: 6 additions & 1 deletion physical/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,11 +860,16 @@ func (b *RaftBackend) SetupCluster(ctx context.Context, opts SetupOpts) error {
// StartAsLeader is only set during init, recovery mode, storage migration,
// and tests.
if opts.StartAsLeader {
// ticker is used to prevent memory leak of using time.After in
// for - select pattern.
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
for {
if raftObj.State() == raft.Leader {
break
}

ticker.Reset(10 * time.Millisecond)
select {
case <-ctx.Done():
future := raftObj.Shutdown()
Expand All @@ -873,7 +878,7 @@ func (b *RaftBackend) SetupCluster(ctx context.Context, opts SetupOpts) error {
}

return errors.New("shutdown while waiting for leadership")
case <-time.After(10 * time.Millisecond):
case <-ticker.C:
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion vault/raft.go