镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
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
3 changes: 3 additions & 0 deletions pkg/storage/chunked.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ func (c *chunkedSecrets) decodeRelease(ctx context.Context, indexSecret *corev1.
}

pr, pw := io.Pipe()
// Close the read side when decoding returns so that the writer goroutine
// does not block forever if decoding stops before all chunk data is read.
defer pr.Close()
Comment on lines +401 to +403

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd try to keep uses of pr together, and put it after the following go func(), right before the gzip.NewReader(pr) call below.

go func() {
defer pw.Close()
firstChunkData, ok := indexSecret.Data["chunk"]
Expand Down
33 changes: 33 additions & 0 deletions pkg/storage/chunked_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"maps"
"runtime"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -95,6 +97,26 @@ var _ = Describe("chunkedSecrets", func() {
_, err := maxReadDriver.Get(releaseKey(rel))
Expect(err).To(MatchError(ContainSubstring("release too large")))
})
It("should not leak goroutines if the release fails to decode", func() {
corruptIndex := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: "corrupt-release.v1"},
Type: SecretTypeChunkedIndex,
Data: map[string][]byte{
"extraChunks": []byte("[]"),
// Larger than the gzip reader's read buffer, so the
// reader stops before consuming all of the chunk data.
"chunk": make([]byte, 10*chunkSize),
},
}
_, err := secretInterface.Create(context.Background(), corruptIndex, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())

for range 5 {
_, err := chunkedDriver.Get(corruptIndex.Name)
Expect(err).To(MatchError(ContainSubstring("failed to create gzip reader")))
}
Eventually(countDecodeReleaseGoroutines).Should(BeZero())
})
})

var _ = Describe("Update", func() {
Expand Down Expand Up @@ -327,6 +349,17 @@ func verifySecrets(secretInterface clientcorev1.SecretInterface, expected int) {
Expect(actualExtraChunkNames).To(ConsistOf(expectedExtraChunkNames))
}

func countDecodeReleaseGoroutines() int {
buf := make([]byte, 1<<20)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
return strings.Count(string(buf[:n]), "(*chunkedSecrets).decodeRelease.func")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice way to make sure the fix worked, but I'm afraid it will be fragile. A change to the way stack is formatted, or changing the name of the type or decodeRelease name or structure will invalidate this count, and silently always return 0, rendering the test useless.

I cannot think of a better way to do this, though. So we could either keep it and accept the above, or drop the test before merging given it's fulfilled its primary purpose.

}
buf = make([]byte, 2*len(buf))
}
}

func releaseKey(rel *release.Release) string {
return fmt.Sprintf("%s.v%d", rel.Name, rel.Version)
}
Expand Down
Loading