Skip to content

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

Closed
master255 opened this issue Dec 23, 2023 · 17 comments
Closed

encoding/json: UnmarshalJSON causes panic #64850

master255 opened this issue Dec 23, 2023 · 17 comments
Labels
OS-Android WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@master255
Copy link

master255 commented Dec 23, 2023

Go version

1.21.1

What operating system and processor architecture are you using (go env)?

Android

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

panic(phasePanicMsg)

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.

@master255 master255 changed the title decoding/json: UnmarshalJSON decoding/json: UnmarshalJSON causes panic Dec 23, 2023
@odeke-em odeke-em changed the title decoding/json: UnmarshalJSON causes panic encoding/json: UnmarshalJSON causes panic Dec 23, 2023
@odeke-em
Copy link
Member

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.

@odeke-em odeke-em added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. OS-Android labels Dec 23, 2023
@master255
Copy link
Author

No. That's impossible. Panic triggers unsteadily and I don't know what it's related to.

@seankhliao
Copy link
Member

seems like a problem of your code rather than the standard library.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 23, 2023
@master255
Copy link
Author

@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?

@seankhliao
Copy link
Member

it violates invariants of the system that make it unsafe to continue, a panic is appropriate.

@master255
Copy link
Author

@seankhliao No, it's not. It is necessary that json.Unmarshal does not cause panic. How do I do that?

@master255
Copy link
Author

@seankhliao Are you a programmer without logic?

@atdiar
Copy link

atdiar commented Dec 24, 2023

@master255 Please respect the code of conduct. Thanks.

@master255
Copy link
Author

@atdiar Maybe you can explain what's going on here?
I cite a significant bug in the code, but no one wants to fix it.
The code is developed as people want it, not as logic dictates?
Is logic used in Golang development?

@atdiar
Copy link

atdiar commented Dec 24, 2023

@master255

If the implementation has an error, there is no point in returning an error.
The system is broken. It's better to panic then.
That's what this panic does I believe.
Errors are for things that should be possible such as an input that can't be processed because it has a mistake.

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?

@master255
Copy link
Author

master255 commented Dec 24, 2023

@atdiar I want a very simple thing - not to get panic in json.Unmarshal. That my application won't completely terminate because of it.
All other languages have try catch and that fixes the problem.
Golang has no try catch. So there should be no panic here if an error is supposed to be returned. Under any circumstances!
Even if the data is being edited while json.Unmarshal is happening.
I want to get an error and ignore it or react somehow.
And I'm very surprised that such simple things it is necessary to explain to people here. This is something new.

@atdiar
Copy link

atdiar commented Dec 24, 2023

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.
If you have a race in your code, might as well panic too.

@master255
Copy link
Author

@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.
So it is not logical to cause panic for a certain kind of error and not for another kind of error.

@atdiar
Copy link

atdiar commented Dec 25, 2023

Panics are recoverable.
If that's really what you need, I recommend reading:

https://go.dev/blog/defer-panic-and-recover

@master255
Copy link
Author

master255 commented Dec 25, 2023

@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?

@atdiar
Copy link

atdiar commented Dec 25, 2023

Perhaps that you can use a closure to call your json.Unmarshal

@seankhliao
Copy link
Member

For questions please refer to https://github.com/golang/go/wiki/Questions

@golang golang locked as off-topic and limited conversation to collaborators Dec 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
OS-Android WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants