2

I'm using Spring Boot with RabbitMQ. In some cases my consumer threads (SimpleMessageListenerContainer) get aborted because of StackOverflowError. I was thinking of catching these kind of errors in order to save the threads and not having to restart my application every time this happens.

  1. Is a thread that went through that error in a state that can keep working or should I let it die?
  2. If I should let it die, how can I replenish the pool thread used by org.springframework.amqp.rabbit?

I know that in general we shouldn't catch Error throwables, but I want to know what would happen in this particular case if I did.

Update

In the end I just performed the actions that would cause such a behaviour in a separate thread using Future and masked (catch and rethrow) any throwable as a RuntimeException. This way I could handle the exception in my consumer thread that remained clean. Not the most performant way of dealing with this, but it works great so far and performance has not been an issue.

Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
  • What is the root cause for the `StackOverflowError`s? –  Dec 05 '16 at 10:45
  • 1
    Looks like an X Y problem to me, rather than trying to catch `StackOverflowError`, you should try to find the root cause and stop it from happening. A `StackOverflowError` is not a normal thing to happen. – g00glen00b Dec 05 '16 at 10:47
  • The root cause are regexes. Of course I will investigate and fix the problem for that particular input, but that won't protect the stability of my application for inputs that I haven't thought of. I'm just trying to be proactive. – Alkis Kalogeris Dec 05 '16 at 10:48
  • 1
    If it's due to the regex being applied on a very large string, perhaps a better solution would be to increase the stack size with the `-Xss` flag. This allows you to keep your application running normally, though you will have to find out what the proper stack size should be. See also https://stackoverflow.com/questions/7509905/java-lang-stackoverflowerror-while-using-a-regex-to-parse-big-strings – g00glen00b Dec 05 '16 at 10:53
  • I'm already doing this. Thank you for the suggestion – Alkis Kalogeris Dec 05 '16 at 10:54
  • 2
    I think it's no good idea to catch an *Error* (except for logging and rethrow). Have a look at this other question: [Differences between Exception and Error](http://stackoverflow.com/questions/912334/differences-between-exception-and-error). – Markus Mitterauer Dec 05 '16 at 11:34

0 Answers0