Skip to content
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

Ensure we close HTTP connections on all paths #2017

Merged
merged 2 commits into from
Jun 29, 2023
Merged
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
1 change: 1 addition & 0 deletions copy/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func compareImageDestinationManifestEqual(ctx context.Context, options *Options,
logrus.Debugf("Unable to create destination image %s source: %v", dest.Reference(), err)
return false, nil, "", "", nil
}
defer destImageSource.Close()

destManifest, destManifestType, err := destImageSource.GetManifest(ctx, targetInstance)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions docker/docker_image_src_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ location = "@REGISTRY@/with-mirror"
DockerInsecureSkipTLSVerify: types.OptionalBoolTrue,
})
require.NoError(t, err, c.input)
defer src.Close()

// The observable behavior
assert.Equal(t, "//"+c.input, src.Reference().StringWithinTransport(), c.input)
Expand Down
3 changes: 2 additions & 1 deletion oci/archive/oci_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,9 @@ func TestReferenceNewImage(t *testing.T) {
func TestReferenceNewImageSource(t *testing.T) {
ref, tmpTarFile := refToTempOCIArchive(t)
defer os.RemoveAll(tmpTarFile)
_, err := ref.NewImageSource(context.Background(), nil)
src, err := ref.NewImageSource(context.Background(), nil)
assert.NoError(t, err)
defer src.Close()
}

func TestReferenceNewImageDestination(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions oci/layout/oci_src_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestGetBlobForRemoteLayers(t *testing.T) {
cache := memory.New()

imageSource := createImageSource(t, &types.SystemContext{})
defer imageSource.Close()
layerInfo := types.BlobInfo{
Digest: digest.FromBytes([]byte("Hello world")),
Size: -1,
Expand All @@ -69,6 +70,7 @@ func TestGetBlobForRemoteLayersWithTLS(t *testing.T) {
imageSource := createImageSource(t, &types.SystemContext{
OCICertPath: "fixtures/accepted_certs",
})
defer imageSource.Close()
cache := memory.New()

layer, size, err := imageSource.GetBlob(context.Background(), types.BlobInfo{
Expand All @@ -85,6 +87,7 @@ func TestGetBlobForRemoteLayersOnTLSFailure(t *testing.T) {
imageSource := createImageSource(t, &types.SystemContext{
OCICertPath: "fixtures/rejected_certs",
})
defer imageSource.Close()
cache := memory.New()
layer, size, err := imageSource.GetBlob(context.Background(), types.BlobInfo{
URLs: []string{httpServerAddr},
Expand Down
3 changes: 2 additions & 1 deletion oci/layout/oci_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ func TestReferenceNewImage(t *testing.T) {

func TestReferenceNewImageSource(t *testing.T) {
ref, _ := refToTempOCI(t)
_, err := ref.NewImageSource(context.Background(), nil)
src, err := ref.NewImageSource(context.Background(), nil)
assert.NoError(t, err)
defer src.Close()
}

func TestReferenceNewImageDestination(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func newOpenshiftClient(ref openshiftReference) (*openshiftClient, error) {
}, nil
}

func (c *openshiftClient) close() {
c.httpClient.CloseIdleConnections()
}

// doRequest performs a correctly authenticated request to a specified path, and returns response body or an error object.
func (c *openshiftClient) doRequest(ctx context.Context, method, path string, requestBody []byte) ([]byte, error) {
requestURL := *c.baseURL
Expand Down
4 changes: 3 additions & 1 deletion openshift/openshift_dest.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func (d *openshiftImageDestination) Reference() types.ImageReference {

// Close removes resources associated with an initialized ImageDestination, if any.
func (d *openshiftImageDestination) Close() error {
return d.docker.Close()
err := d.docker.Close()
d.client.close()
return err
}

func (d *openshiftImageDestination) SupportedManifestMIMETypes() []string {
Expand Down
9 changes: 5 additions & 4 deletions openshift/openshift_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ func (s *openshiftImageSource) Reference() types.ImageReference {

// Close removes resources associated with an initialized ImageSource, if any.
func (s *openshiftImageSource) Close() error {
var err error
if s.docker != nil {
err := s.docker.Close()
err = s.docker.Close()
s.docker = nil

return err
}

return nil
s.client.close()

return err
}

// GetManifest returns the image's manifest along with its MIME type (which may be empty when it can't be determined but the manifest is available).
Expand Down
3 changes: 2 additions & 1 deletion ostree/ostree_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ func TestReferenceNewImage(t *testing.T) {
func TestReferenceNewImageSource(t *testing.T) {
ref, err := Transport.ParseReference("busybox")
require.NoError(t, err)
_, err = ref.NewImageSource(context.Background(), nil)
src, err = ref.NewImageSource(context.Background(), nil)
require.NoError(t, err)
defer src.Close()
}

func TestReferenceNewImageDestination(t *testing.T) {
Expand Down