-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
pkg/types: return error instead of crashing when code supplies an incompatible type to types.LoadArgs() #460
Conversation
types.LoadArgs does an unsafe typecast for env args key value pairs. This can cause a panic if the config type fields do not implement the encoding.TextUnmarshaler interface. This commit modifies it to do a safe type cast and return an error in such scenarios.
What is the use-case where panic is an unsuitable response? |
@bboreham as a plugin developer, it'd be a lot easier to deal with errors than panics when the config parsing logic in the plugin does not handle unmarshalling of textual form of the object. It provides an avenue to plugin custom logic when such errors occur. For example, the plugin might choose to apply defaults or handle it more gracefully with a more meaningful error message to the invoker rather than just emitting a stack trace of the panic. It plays nicely with the existing logic since the return type includes an error already. |
But this is not what you changed. You changed the case where a plugin developer passes an object into
@aaithal if you wanted to do that then you'd need a custom error type, not But I do not buy this at all. You are trying to provide a run-time solution to a logic error that occurred before compile-time. |
Since these arguments are set via the
Since the arguments are passed by the user using |
In your example, what values can be passed in |
If the type is Taking a step back, my thought was that if there's an error that you know can be caused and you can detect it, it's better to detect and return the same rather than a panic as it can be gracefully handled (For example, may be the plugin can cleanup resources if it has created any). If that's not what we want to do here and the intention is to make it panic rather than return an error, I'm fine with that too (just means that I misunderstood the intent). |
You can write code to handle a panic in Go, if you have essential cleanup to do. I argue that panic is fine for this case because it is a logic error that will be caught by the developer's testing, not something that would be expected at runtime. |
I'm not arguing that you can't handle panics in go. My point is that error handling is preferred over handling panic's in go.
If your point is that the onus is on the developer to ensure that all of their types implement the text unmarshaller, I agree. However, this only becomes apparent if you take a look at the I can make this a typed error instead of |
@aaithal I would be happy with a comment or other documentation making clear the requirements on Not happy with complicating the code for developers who don't test all their arguments. If you can explain the case where a developer can test the expected behaviour and yet get caught by this panic, I'm listening. |
@sosans1357 your messages seem disruptive. Please explain what you are doing or stop. |
Given that the
|
@squeed thanks for taking a look and for the comment. Do you also want this to be a named error or can I stop after making the error message more descriptive? |
I personally don't care if it's an explicit error type, but @bboreham is welcome to disagree :-). |
…ble condition is detected
@squeed I've updated the PR with a more descriptive error message. Can you please take another look? Thanks! |
I did not advocate for a custom error type; I observed that this was one mis-match between the claimed benefit and the code proposed. Can we adjust the title and the claimed benefit to match whatever is the aim now? Is it just to shorten the plugin developer's debugging process? |
Summary
types.LoadArgs()
does an unsafe typecast for env args key value pairs.This can cause a panic if the config type fields do not implement
the
encoding.TextUnmarshaler
interface. This commit modifies it todo a safe type cast and return an error in such scenarios.
Testing
./test.sh
succeeds as wellReference
$ go test ./pkg/types/ ok github.com/containernetworking/cni/pkg/types 0.005s