-
Notifications
You must be signed in to change notification settings - Fork 379
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
Fix a http.response.Body leak on a permission error #2363
Merged
Merged
Conversation
This file contains 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
mtrmac
added
the
kind/bug
A defect in an existing functionality (or a PR fixing it)
label
Apr 5, 2024
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č <[email protected]>
Not doing so can keep the HTTP client code waiting forever, keeping some goroutines running, and the socket open. Signed-off-by: Miloslav Trmač <[email protected]>
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. |
LGTM |
/approve |
giuseppe
approved these changes
Apr 8, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This was referenced Apr 9, 2024
Merged
Merged
Merged
Merged
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
needsRetryWithUpdatedScope
code path, we didn’t close the response body.See individual commit messages for details.