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

Need to support panic recovery in the request handlers #205

Open
ysaakpr opened this issue Jun 26, 2023 · 4 comments
Open

Need to support panic recovery in the request handlers #205

ysaakpr opened this issue Jun 26, 2023 · 4 comments

Comments

@ysaakpr
Copy link

ysaakpr commented Jun 26, 2023

go func() {
switch action {
case core.BootNotificationFeatureName:
confirmation, err = cs.coreHandler.OnBootNotification(chargePoint.ID(), request.(*core.BootNotificationRequest))
case core.AuthorizeFeatureName:
confirmation, err = cs.coreHandler.OnAuthorize(chargePoint.ID(), request.(*core.AuthorizeRequest))
case core.DataTransferFeatureName:
confirmation, err = cs.coreHandler.OnDataTransfer(chargePoint.ID(), request.(*core.DataTransferRequest))

What happen if a panic happens in a request handler in this go routine?

My server keep re starting since its a go routine, and my request level panic handler not working here. Need a way to handle the panic situations. Or have proper panic recovery for all the internal go routines.

defer func() {
			r := recover()
			if r != nil {
				var err error
				switch t := r.(type) {
				case string:
					err = errors.New(t)
				case error:
					err = t
				default:
					err = errors.New("Unknown error")
				}
				// sendMeMail(err)
				fmt.Println("recivered panic:", err)
				cs.sendResponse(chargePoint.ID(), confirmation, err, requestId)
			}
		}()

This is what is did in the start of the go routine to fix it.

@lorenzodonini
Copy link
Owner

The callback expects either a response or an error. Currently a panic within the same goroutine will break the request - response flow for that connection (which can only be fixed with a re-connection).

Why does the handler panic anyway?

@ysaakpr
Copy link
Author

ysaakpr commented Jul 4, 2023

the panic will cause the system to exit if not recovered.

Why panic, may be a bad coder/coding. and some libraries cause panic instead of error in its error cases, and unless handled carefully all these can cause the entier server to go down. Which is not a good behaviour

Isn't it better to recover the panic, or give the flexibility to handle a panic at root level, by setting a panic handler.

@lorenzodonini
Copy link
Owner

lorenzodonini commented Jul 4, 2023

My point was simply about "bad coders" being responsible for their own implementation. If a handler is panicking, it should be fixed in there.

That being said your suggestion is definitely a good improvement, I'll add it in a PR.

@ysaakpr
Copy link
Author

ysaakpr commented Jul 5, 2023

My point was simply about "bad coders" being responsible for their own implementation. If a handler is panicking, it should be fixed in there.

Yes... it should be fixed in there, in our case there are many handlers performing ocpp message handling, and there is no common place in the library where i can wrap the logic of panic recovery. From a library perspective we need some handler injection possible at the root level,

That being said your suggestion is definitely a good improvement, I'll add it in a PR.

Thanks for taking it, better to accept it as a panic handler in the configurations of Server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants