Jeffail/tunny

Panic behaviour notes

bharathramh92 opened this issue · 2 comments

May I know how tunny behaves if panic happens inside the NewFunc. Does tunny re-spawn the worker or what is the ideal way to handle panic in a NewFunc worker?

Hey @bharathramh92, ideally you need to capture the panic inside the func. I made a conscious decision to not capture panics within Tunny, as it's not possible for me to make a reasonable generic decision on how to proceed.

Ideally there shouldn't be any chance of a panic inside your pooled func, but if there is I would recommend adding a recover like this:

pool := tunny.NewFunc(1, func(payload interface{}) interface{} {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered", r)
		}
	}()
	panic("foo")
	return payload
})

If you recover a panic in this way then the result returned from a pool.Process call will be nil.

thanks for your response. I used the defer statement like the way you had mentioned.