Skip to content
This repository was archived by the owner on Feb 1, 2021. It is now read-only.

close http response body to avoid potential memory leak #2176

Conversation

allencloud
Copy link
Contributor

@allencloud allencloud commented Apr 27, 2016

It is an amazing thing for Swarm to use engine-api to contact with docker daemon. However in engine-api, after Do a request, sometimes it makes sure that response body is closed via ensureReaderClosed:

err = json.NewDecoder(resp.body).Decode(&dels)
ensureReaderClosed(resp)

In some cases, engine api returns an io.ReadCloser like below:

    resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
    if err != nil {
        return nil, err
    }
    return resp.body, nil

In the case above, Swarm should close the resp.body.

This PR did:

  1. change pullResponse into pullResponseBody to make it more readable, as this is a type of io.ReadCloser or http.Response.Body
  2. close http response body to avoid potential memory leak

Signed-off-by: Sun Hongliang [email protected]

@allencloud allencloud force-pushed the close-http-response-body-to-avoid-memory-leak branch 2 times, most recently from a863c02 to f793a28 Compare April 27, 2016 04:13
@allencloud allencloud force-pushed the close-http-response-body-to-avoid-memory-leak branch from f793a28 to b5a74ee Compare April 27, 2016 04:14
@MHBauer
Copy link
Contributor

MHBauer commented Apr 27, 2016

That is what the docs say.

https://github.com/docker/engine-api/blob/master/client/image_pull.go#L17
https://github.com/docker/engine-api/blob/master/client/image_load.go#L13

I don't see the need to rename pullResponse to pullResponseBody. But that is just my opinion.

LGTM as is.

@allencloud
Copy link
Contributor Author

@MHBauer
Thanks for your providing reference. 👍 :)

@vieux
Copy link
Contributor

vieux commented Apr 29, 2016

LGTM ping @abronan @dongluochen

@abronan
Copy link
Contributor

abronan commented Apr 29, 2016

LGTM

@abronan abronan merged commit dd90e08 into docker-archive:master Apr 29, 2016
@allencloud allencloud deleted the close-http-response-body-to-avoid-memory-leak branch May 4, 2016 01:42
ChristianKniep pushed a commit to ChristianKniep/swarm that referenced this pull request Jul 27, 2017
…ponse-body-to-avoid-memory-leak

close http response body to avoid potential memory leak
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants