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

Fix non-closed http responses #5755

Merged
merged 1 commit into from
Jun 13, 2022
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
7 changes: 1 addition & 6 deletions hack/operatorhub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,7 @@ func makeRequest(url string) (io.Reader, error) {
if err != nil {
return nil, fmt.Errorf("failed to GET %s: %w", url, err)
}

defer func() {
if resp.Body != nil {
resp.Body.Close()
}
}()
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return nil, errNotFound
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ func (r UnmanagedAssociationConnectionInfo) Request(path string, jsonPath string
if err != nil {
return "", err
}
defer resp.Body.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, this one may lead to a [memory|fd|connection] leak. FWIW I wrote a quick and dirty integration test that demonstrates the leak, after a few iterations we have some too many open files errors.

if resp.StatusCode != 200 {
return "", fmt.Errorf("error requesting %q, statusCode = %d", url, resp.StatusCode)
}

defer resp.Body.Close()
var obj interface{}
if err = json.NewDecoder(resp.Body).Decode(&obj); err != nil {
return "", err
Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/common/stackmon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func newBeatConfig(client k8s.Client, beatName string, resource monitoring.HasMo
}

configHash := fnv.New32a()
configHash.Write(configBytes)

_, err = configHash.Write(configBytes)
if err != nil {
return beatConfig{}, err
}

configSecret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test/enterprisesearch/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func (e EnterpriseSearchClient) doRequest(request *http.Request) ([]byte, error)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("http response status code is %d)", resp.StatusCode)
}

defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}

Expand Down
3 changes: 1 addition & 2 deletions test/e2e/test/kibana/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ func DoRequest(k *test.K8sClient, kb kbv1.Kibana, password string, method string
if err != nil {
return nil, errors.Wrap(err, "while doing request")
}

defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, &APIError{
StatusCode: resp.StatusCode,
msg: fmt.Sprintf("fail to request %s, status is %d)", pathAndQuery, resp.StatusCode),
}
}

defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
3 changes: 1 addition & 2 deletions test/e2e/test/maps/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ func DoRequest(client *http.Client, ems v1alpha1.ElasticMapsServer, method, path
if err != nil {
return nil, fmt.Errorf("while making request: %w", err)
}

defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, &APIError{
StatusCode: resp.StatusCode,
msg: fmt.Sprintf("fail to request %s, status is %d)", path, resp.StatusCode),
}
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}