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

Limit and remove panic usage in the code. #7409

Closed
5 tasks
robert-zaremba opened this issue Sep 29, 2020 · 4 comments
Closed
5 tasks

Limit and remove panic usage in the code. #7409

robert-zaremba opened this issue Sep 29, 2020 · 4 comments
Labels
C:errors T:tech debt Tech debt that should be cleaned up Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity.

Comments

@robert-zaremba
Copy link
Collaborator

robert-zaremba commented Sep 29, 2020

Summary

There are many (a lot!) places in the SDK code which are susceptible for panics.
The general Go recommendation is to not use panics whenever possible.

Problem Definition

We should avoid panics whenever possible. The Go team deliberately limited the exception mechanism for good reasons: they are not designed to rigorously check code on each step and can leave an app in a wrong state if not managed properly.

From Go FAQ:

Why does Go not have exceptions?

We believe that coupling exceptions to a control structure, as in the try-catch-finally idiom, results in convoluted code. It also tends to encourage programmers to label too many ordinary errors, such as failing to open a file, as exceptional.
Go takes a different approach. For plain error handling, Go's multi-value returns make it easy to report an error without overloading the return value. A canonical error type, coupled with Go's other features, makes error handling pleasant but quite different from that in other languages.

Rob Pike on a "Proposal for an exception-like mechanism"

This is exactly the kind of thing the proposal tries to avoid. Panic and recover are not an exception mechanism as usually defined because the usual approach, which ties exceptions to a control structure, encourages fine-grained exception handling that makes code unreadable in practice. There really is a difference between an error and what we call a panic, and we want that difference to matter. Consider Java, in which opening a file can throw an exception. In my experience few things are less exceptional than failing to open a file, and requiring me to write inside-out code to handle such a quotidian operation feels like a Procrustean imposition.

Proposal

Look through the code and remove panics wherever possible.

  • in test code, reuse testing.T.Fail / Fatal methods.
  • in SDK code return errors. Search for:
    • panic
    • log.Fatal

Discussions

Incremental issues


For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
@robert-zaremba robert-zaremba added the Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity. label Sep 29, 2020
@alexanderbez
Copy link
Contributor

alexanderbez commented Sep 29, 2020

in test code, reuse testing.T.Fail / Fatal methods.

Where do we explicitly panic in tests?! We shouldn't be doing this. I'm not aware of any places where we do this.

in SDK code return errors.

Yes, obviously returning errors as far upstream as possible is ideal, and there are a few places where we panic that we should not (e.g. CLI, client-code, REST handlers maybe, etc...). BUT, the panics found in the modules (i.e. state-machine) are explicit and meant to panic! Those are not to be removed.

@robert-zaremba
Copy link
Collaborator Author

Here is a list of places where we panic in tests: https://pastebin.com/jKmQv3Ug

Here is the list of all places where we panic. It's huge. Mostly in modules, but also in baseapp, codec, crypto (sic!)... https://pastebin.com/TkNEyq9x

@alexanderbez
Copy link
Contributor

Cool, thanks for compiling that. We should definitely address the tests.

@tac0turtle
Copy link
Member

closing in favour of #15555

@github-project-automation github-project-automation bot moved this from 📝 Todo to 👏 Done in Cosmos-SDK Sep 11, 2023
@github-project-automation github-project-automation bot moved this from Ready to Done in Cosmos SDK Maintenance Sep 11, 2023
@tac0turtle tac0turtle removed this from Cosmos-SDK Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:errors T:tech debt Tech debt that should be cleaned up Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity.
Projects
No open projects
Development

No branches or pull requests

3 participants