Skip to content

Commit 9946ec8

Browse files
authored
fix(plugin): fix memory leak when following logs with kubectl cnpg logs -f (#10976)
Running `kubectl cnpg logs -f` on a busy cluster would cause the plugin process to grow in memory without bound over time. Each log group allocated a new timer that was never released until the stream ended, so memory accumulated for as long as the follow was running. Timers are now reused across iterations, keeping memory usage stable regardless of how long the log stream runs. Closes #10893 Signed-off-by: Anand-240 <[email protected]>
1 parent 83287c2 commit 9946ec8

3 files changed

Lines changed: 148 additions & 2 deletions

File tree

‎internal/cmd/plugin/logs/pretty/pretty.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,17 @@ func (bf *prettyCmd) group(ctx context.Context, logChannel <-chan logRecord, gro
159159
buffer = bufferArray[0:0]
160160
}
161161

162+
// A single timer is reused across iterations and reset at the top of each
163+
// one. This preserves the original "flush the buffer after one second of
164+
// inactivity" behaviour while avoiding the allocation of a new timer (and a
165+
// piled-up deferred Stop) on every received log record, which previously
166+
// grew unbounded for the lifetime of the stream.
167+
timer := time.NewTimer(1 * time.Second)
168+
defer timer.Stop()
169+
162170
logLoop:
163171
for {
164-
timer := time.NewTimer(1 * time.Second)
165-
defer timer.Stop()
172+
timer.Reset(1 * time.Second)
166173

167174
select {
168175
case <-ctx.Done():
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Copyright © contributors to CloudNativePG, established as
3+
CloudNativePG a Series of LF Projects, LLC.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
package pretty
21+
22+
import (
23+
"context"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
)
28+
29+
var _ = Describe("group", func() {
30+
// collectGroups feeds the given records into group() and returns the
31+
// emitted groups once the input channel is closed.
32+
collectGroups := func(bf *prettyCmd, records []logRecord) [][]logRecord {
33+
GinkgoHelper()
34+
35+
logChannel := make(chan logRecord)
36+
groupChannel := make(chan []logRecord)
37+
38+
go bf.group(context.Background(), logChannel, groupChannel)
39+
40+
go func() {
41+
defer GinkgoRecover()
42+
for _, record := range records {
43+
logChannel <- record
44+
}
45+
close(logChannel)
46+
}()
47+
48+
var groups [][]logRecord
49+
for group := range groupChannel {
50+
groups = append(groups, group)
51+
}
52+
return groups
53+
}
54+
55+
makeRecords := func(messages ...string) []logRecord {
56+
records := make([]logRecord, len(messages))
57+
for i, msg := range messages {
58+
records[i] = logRecord{Msg: msg}
59+
}
60+
return records
61+
}
62+
63+
It("emits a full group as soon as groupSize records are buffered", func() {
64+
bf := &prettyCmd{groupSize: 2}
65+
66+
groups := collectGroups(bf, makeRecords("a", "b", "c", "d"))
67+
68+
Expect(groups).To(HaveLen(2))
69+
Expect(groups[0]).To(HaveLen(2))
70+
Expect(groups[0][0].Msg).To(Equal("a"))
71+
Expect(groups[0][1].Msg).To(Equal("b"))
72+
Expect(groups[1][0].Msg).To(Equal("c"))
73+
Expect(groups[1][1].Msg).To(Equal("d"))
74+
})
75+
76+
It("flushes the remaining partial buffer when the input is closed", func() {
77+
bf := &prettyCmd{groupSize: 10}
78+
79+
groups := collectGroups(bf, makeRecords("a", "b", "c"))
80+
81+
Expect(groups).To(HaveLen(1))
82+
Expect(groups[0]).To(HaveLen(3))
83+
Expect(groups[0][0].Msg).To(Equal("a"))
84+
Expect(groups[0][2].Msg).To(Equal("c"))
85+
})
86+
87+
It("emits nothing and closes the output when no records are received", func() {
88+
bf := &prettyCmd{groupSize: 4}
89+
90+
groups := collectGroups(bf, nil)
91+
92+
Expect(groups).To(BeEmpty())
93+
})
94+
95+
It("terminates and closes the output channel when the context is cancelled", func() {
96+
bf := &prettyCmd{groupSize: 4}
97+
98+
ctx, cancel := context.WithCancel(context.Background())
99+
logChannel := make(chan logRecord)
100+
groupChannel := make(chan []logRecord)
101+
102+
go bf.group(ctx, logChannel, groupChannel)
103+
cancel()
104+
105+
Eventually(groupChannel).Should(BeClosed())
106+
})
107+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright © contributors to CloudNativePG, established as
3+
CloudNativePG a Series of LF Projects, LLC.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
package pretty
21+
22+
import (
23+
"testing"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
)
28+
29+
func TestPretty(t *testing.T) {
30+
RegisterFailHandler(Fail)
31+
RunSpecs(t, "Pretty Suite")
32+
}

0 commit comments

Comments
 (0)