-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: UnmarshalJSON causes panic #64850
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
Comments
Thank you for filing this issue @master255! Your report currently is not actionable because it provides a single line of code without context of what could be going wrong, nor the stacktrace of the panic. Could you please provide a minimized complete runnable reproducer for the panic as well as the stacktrace? Thank you. |
No. That's impossible. Panic triggers unsteadily and I don't know what it's related to. |
seems like a problem of your code rather than the standard library. |
@seankhliao But here in the code you can see that panic is called, and the method returns an error and should not call panic. Why do you ignore it? |
it violates invariants of the system that make it unsafe to continue, a panic is appropriate. |
@seankhliao No, it's not. It is necessary that json.Unmarshal does not cause panic. How do I do that? |
@seankhliao Are you a programmer without logic? |
@master255 Please respect the code of conduct. Thanks. |
@atdiar Maybe you can explain what's going on here? |
If the implementation has an error, there is no point in returning an error. Panic (here) is for when the processing itself encountered an impossible value which means somehow the implementation allows for an impossible state. That's why it is surprising that you come across such a panic. As @odeke-em said, perhaps you can provide a stacktrace, reproducer for the panic you receive. It might be unrelated to this code as @seankhliao mentions. If you read the code, it says: // phasePanicMsg is used as a panic message when we end up with something that
// shouldn't happen. It can indicate a bug in the JSON decoder, or that
// something is editing the data slice while the decoder executes. So you could investigate the latter part, perhaps. Maybe you have a race? |
@atdiar I want a very simple thing - not to get panic in json.Unmarshal. That my application won't completely terminate because of it. |
If the unmarshalling panics, you have a bigger problem than it not returning an error. That's the point of panicking here. If your code is correct, it should never even hit that case so this is not something to worry about. |
@atdiar try catch catches any problem. What makes you think there are big and small problems at all? All problems are equal. If even a small error occurs, the application stops working. Just like when a big error occurs. |
Panics are recoverable. |
@atdiar But it is necessary for me to ignore the error and continue executing the program (method). Not to catch the error. How do I do that? |
Perhaps that you can use a closure to call your json.Unmarshal |
For questions please refer to https://github.com/golang/go/wiki/Questions |
Go version
1.21.1
What operating system and processor architecture are you using (
go env
)?What did you do?
err := json.Unmarshal(bootstrap, &peersAddrs)
What did you expect to see?
Get the error in the err variable
What did you see instead?
panic in this line
go/src/encoding/json/decode.go
Line 360 in 2184a39
This method (and maybe other) should not cause a panic, but should return an error.
Otherwise, I hope for quiet operation of the method, but it causes the whole application to crash.
The text was updated successfully, but these errors were encountered: