Skip to content
Open
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
47 changes: 47 additions & 0 deletions manager/quiescence_leak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright IBM Corp. 2014, 2025
// SPDX-License-Identifier: MPL-2.0

package manager

import (
"runtime"
"strings"
"testing"
"time"

"github.com/hashicorp/consul-template/config"
)

// The timer goroutine must be able to exit when nothing is receiving from the
// quiescence channel any more, as happens after the runner stops.
func TestRunner_quiescenceTimerExitsWithoutReceiver(t *testing.T) {
c := config.DefaultConfig().Merge(&config.Config{
Templates: &config.TemplateConfigs{
&config.TemplateConfig{Contents: config.String("hello")},
},
})
c.Finalize()

r, err := NewRunner(c, true)
if err != nil {
t.Fatal(err)
}

q := newQuiescence(r.quiescenceCh, time.Millisecond, 5*time.Millisecond, r.templates[0])
q.tick()
r.Stop()

const timerGoroutine = "manager.(*quiescence).tick."
deadline := time.Now().Add(2 * time.Second)
for {
buf := make([]byte, 1<<20)
stacks := string(buf[:runtime.Stack(buf, true)])
if !strings.Contains(stacks, timerGoroutine) {
return
}
if time.Now().After(deadline) {
t.Fatal("quiescence timer goroutine is still blocked after the runner stopped")
}
time.Sleep(10 * time.Millisecond)
}
}
4 changes: 4 additions & 0 deletions manager/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,10 @@ func (r *Runner) init(clients *dep.ClientSet) error {
// back into an array of templates.
r.templates = templates

// Each template has at most one pending quiescence, so with one slot per
// template a timer can always send, even after the runner has stopped.
r.quiescenceCh = make(chan *template.Template, len(templates))

r.renderEvents = make(map[string]*RenderEvent, numTemplates)

if *r.config.Dedup.Enabled {
Expand Down