Fix a http.response.Body leak on a permission error#2363
Merged
giuseppe merged 2 commits intocontainers:mainfrom Apr 8, 2024
Merged
Fix a http.response.Body leak on a permission error#2363giuseppe merged 2 commits intocontainers:mainfrom
giuseppe merged 2 commits intocontainers:mainfrom
Conversation
The idea of a StatusTooManyRequests retry loop, or the needsRetryWithUpdatedScope logic, only makes sense if we do get a response; on other errors, we can exit immediately. So do that, and simplify the code. Should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Not doing so can keep the HTTP client code waiting forever, keeping some goroutines running, and the socket open. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Collaborator
Author
|
Testing this with package main
import (
"context"
"fmt"
"io/fs"
"os"
"runtime/debug"
"github.com/containers/image/v5/docker"
"github.com/containers/image/v5/types"
"github.com/containers/image/v5/version"
"github.com/sirupsen/logrus"
)
func inspectImage(ref types.ImageReference) error {
ctx := context.Background()
src, err := ref.NewImageSource(ctx, nil)
if err != nil {
return err
}
defer src.Close()
_, _, err = src.GetManifest(ctx, nil)
return err
}
func listOpenFiles() {
fd, err := os.Open("/dev/fd")
if err != nil {
panic(err)
}
defer fd.Close()
entries, err := fd.ReadDir(-1)
fmt.Printf("%d open files:\n", len(entries))
for _, e := range entries {
fmt.Printf("\t%s\n", fs.FormatDirEntry(e))
}
}
func main() {
debug.SetTraceback("all")
c := make(chan struct{})
go func() { <-c }() // A sanity check to show the traceback does list all goroutines
fmt.Printf("c/image version: %s\n", version.Version)
logrus.SetLevel(logrus.DebugLevel)
ref, err := docker.ParseReference("//docker.io/library/none:latest")
if err != nil {
panic(err)
}
for i := 0; i < 10; i++ {
_ = inspectImage(ref)
}
listOpenFiles()
panic("listing goroutines:")
}before this PR, I see with 10 After this PR, it’s just the first two goroutines, as expected. |
Member
|
LGTM |
Member
|
/approve |
This was referenced Apr 9, 2024
Merged
Merged
Merged
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On the
needsRetryWithUpdatedScopecode path, we didn’t close the response body.See individual commit messages for details.