Skip to content

Minor false positive when response body is passed to deferred function as parameter #60

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

Open
pellared opened this issue Jul 31, 2024 · 3 comments

Comments

@pellared
Copy link

pellared commented Jul 31, 2024

I got a minor false positive for a deferred response close like below:

	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			log.Printf("failed to close http response body, %v\n", err)
		}
	}(res.Body)

The workaround was to refactor to:

	defer func() {
		err := res.Body.Close()
		if err != nil {
			log.Printf("failed to close http response body, %v\n", err)
		}
	}()

Tested with https://github.com/golangci/golangci-lint/releases/tag/v1.59.1 and https://github.com/open-telemetry/opentelemetry-go-contrib.
PR for reference: open-telemetry/opentelemetry-go-contrib#5962

@pellared
Copy link
Author

Might be related to #51

pellared added a commit to open-telemetry/opentelemetry-go-contrib that referenced this issue Aug 1, 2024
Add https://golangci-lint.run/usage/linters/#bodyclose linter.

Fix a few issues in tests detected by the linter.

False positives reported:
- timakin/bodyclose#59
- timakin/bodyclose#60
luca-filipponi pushed a commit to luca-filipponi/opentelemetry-go-contrib that referenced this issue Aug 9, 2024
Add https://golangci-lint.run/usage/linters/#bodyclose linter.

Fix a few issues in tests detected by the linter.

False positives reported:
- timakin/bodyclose#59
- timakin/bodyclose#60
@tisonkun
Copy link

@timakin This is quite a big downside for other project adopt it. As per #50 I add a helper function:

// sneakyBodyClose closes the body and ignores the error.
// This is useful to close the HTTP response body when we don't care about the error.
func sneakyBodyClose(body io.ReadCloser) {
	if body != nil {
		_ = body.Close()
	}
}

And now bodyclose cannot find defer sneakyBodyClose(resp.Body) actually properly close the body.

@arvenil
Copy link

arvenil commented May 12, 2025

Same problem is you do want to do both, close the body and log the error:

package closer

import (
	"context"
	"io"

	"github.com/rs/zerolog/log"
)

func Close(ctx context.Context, closer io.Closer) {
	if closer != nil {
		if err := closer.Close(); err != nil {
			log.Ctx(ctx).Warn().Err(err).Msg("close response body")
		}
	}
}
defer closer.Close(ctx, resp.Body)
response body must be closed (bodyclose)

Alternative is to keep duplicating hundreds of lines or keep ignoring the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants