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

Failures in deferred calls not captured #4586

Closed
stmcginnis opened this issue May 8, 2021 · 10 comments · Fixed by #5106
Closed

Failures in deferred calls not captured #4586

stmcginnis opened this issue May 8, 2021 · 10 comments · Fixed by #5106
Assignees
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/bug Categorizes issue or PR as related to a bug. priority/backlog Higher priority than priority/awaiting-more-evidence.

Comments

@stmcginnis
Copy link
Contributor

What steps did you take and what happened:

Great suggestion from Stefan in #4499 (comment)

We're probably doing this all across the code base. But I'm a bit concerned about just ignoring errors in those cases. I know
it's not that critical in tests but let's discuss it :)

Can you take a look at: https://github.com/thanos-io/thanos/blob/main/docs/contributing/coding-style-guide.md#reliability

WDYT about the pattern they are using with defer runutil.CloseWithErrCapture(&err, f, "close file") ?

I think it would be a good idea to use something like that to be able to capture failure information in deferred calls.

What did you expect to happen:

It would be great to capture failure information rather than silently swallowing problems.

Anything else you would like to add:
[Miscellaneous information that will assist in solving the issue.]

Environment:

  • Cluster-api version:
  • Minikube/KIND version:
  • Kubernetes version: (use kubectl version):
  • OS (e.g. from /etc/os-release):

/kind bug

/assign

@k8s-ci-robot k8s-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label May 8, 2021
@vincepri
Copy link
Member

Doesn't the linter complain if the error is ignored?

@sbueringer
Copy link
Member

sbueringer commented May 10, 2021

@vincepri No, at least not yet. Although I'm not sure if there's a linter we could enable via golangci-lint

Errors in defer calls are pretty frequently ignored (not sure why), especially for things like file.Close.

Hm strange I think errcheck should actually detect those issue. Found a few rejcted issues in the linter repo of folks wo wanted to disable the linter on defer calls:

The search is not ideal, but even at a first glance we have a few occurences of ignored errors in defer Close statements: https://github.com/kubernetes-sigs/cluster-api/search?q=defer+Close

@vincepri
Copy link
Member

vincepri commented Jul 6, 2021

/milestone Next
/priority backlog
/help

@k8s-ci-robot
Copy link
Contributor

@vincepri:
This request has been marked as needing help from a contributor.

Please ensure the request meets the requirements listed here.

If this request no longer meets these requirements, the label can be removed
by commenting with the /remove-help command.

In response to this:

/milestone Next
/priority backlog
/help

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added this to the Next milestone Jul 6, 2021
@k8s-ci-robot k8s-ci-robot added priority/backlog Higher priority than priority/awaiting-more-evidence. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. labels Jul 6, 2021
@sbueringer
Copy link
Member

sbueringer commented Jul 8, 2021

Doesn't the linter complain if the error is ignored?

Apparently we have a linter for it. gosec finding G307 detects some of these cases. The findings we have there seem reasonable: G307: Deferring unsafe method "Close" on type "*os.File (9x). I think if we want to address those we should probably do it similarly as Thanos here: https://github.com/thanos-io/thanos/blob/main/docs/contributing/coding-style-guide.md#defers-dont-forget-to-check-returned-errors (CloseWithErrCapture )

(@vincepri)

@vincepri
Copy link
Member

@sbueringer Is there any action items we have as part of this issue? If we can rework it to be more specific and provide a potential solution, we can also mark it as good-first-issue

@sbueringer
Copy link
Member

sbueringer commented Aug 11, 2021

@vincepri In my opinion we have two options depending on if we want to fix G307: Deferring unsafe method "Close" on type "*os.File:

  1. if not: we should move the exclude of G307 above # The following are being worked on to remove their exclusion. This list should be reduced or go away all together over time. in .golangci.yml

  2. if yes: I would propose to implement it as described in the Thanos style guide by implementing a CloseWithErrCapture util func which we can then use like this: defer runutil.CloseWithErrCapture(&err, f, "close file")

I think option 1 might not be worth it to flag it as good-first-issue as it's just moving one line in the linter config. Option 2 would be good for good-first-issue. We just should decide first :)

@vincepri
Copy link
Member

I'm ok to leave that as-is for now, file closure failure are usually a sign for other things failing way earlier, although if we want to fix it that's also fine

@sbueringer
Copy link
Member

Same for me. Let's see if @CecileRobertMichon or @fabriziopandini have a strong opinion on that. Otherwise I would adjust the linter config and close the issue.

@fabriziopandini
Copy link
Member

+1 to leave as it is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/bug Categorizes issue or PR as related to a bug. priority/backlog Higher priority than priority/awaiting-more-evidence.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants