Skip to content

Commit

Permalink
Fix leak when find client returns not found (#137)
Browse files Browse the repository at this point in the history
* Fix leak when find client returns not found

The find client is not closing the response body when it gets a not found response. This PR fixes that.

* Make sure response body is read to EOF and closed
* Remove redundant response body close
  • Loading branch information
gammazero authored Nov 21, 2023
1 parent 81798c1 commit 9524bdc
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
3 changes: 3 additions & 0 deletions dagsync/ipnisync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ nextURL:
case http.StatusOK:
return cb(resp.Body)
case http.StatusNotFound:
io.Copy(io.Discard, resp.Body)
if s.plainHTTP && !s.noPath {
// Try again with no path for legacy http.
log.Warnw("Plain HTTP got not found response, retrying without IPNI path for legacy HTTP")
Expand All @@ -321,6 +322,7 @@ nextURL:
// being checked already.
return fmt.Errorf("content not found: %w", ipld.ErrNotExists{})
case http.StatusForbidden:
io.Copy(io.Discard, resp.Body)
if s.plainHTTP && !s.noPath {
// Try again with no path for legacy http.
log.Warnw("Plain HTTP got forbidden response, retrying without IPNI path for legacy HTTP")
Expand All @@ -330,6 +332,7 @@ nextURL:
}
fallthrough
default:
io.Copy(io.Discard, resp.Body)
return fmt.Errorf("non success http fetch response at %s: %d", fetchURL.String(), resp.StatusCode)
}
}
Expand Down
4 changes: 3 additions & 1 deletion find/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ func (c *Client) Find(ctx context.Context, m multihash.Multihash) (*model.FindRe
if err != nil {
return nil, err
}
defer resp.Body.Close()

// Handle failed requests
if resp.StatusCode != http.StatusOK {
io.Copy(io.Discard, resp.Body)
if resp.StatusCode == http.StatusNotFound {
return &model.FindResponse{}, nil
}
return nil, fmt.Errorf("find query failed: %v", http.StatusText(resp.StatusCode))
}

defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down
6 changes: 2 additions & 4 deletions find/client/dhstore_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ func (d *dhstoreHTTP) FindMultihash(ctx context.Context, dhmh multihash.Multihas
if err != nil {
return nil, err
}

body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -70,10 +69,9 @@ func (d *dhstoreHTTP) FindMetadata(ctx context.Context, hvk []byte) ([]byte, err
if err != nil {
return nil, err
}

body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down
2 changes: 0 additions & 2 deletions pcache/http_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ func (s *httpSource) FetchAll(ctx context.Context) ([]*model.ProviderInfo, error
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()

if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9524bdc

Please sign in to comment.