javaloggingstack-overflow

How to report a StackOverflowError without triggering another StackOverflowError?


I have the following situation:

I have code that is prone to StackOverflowErrors. This will have to be rewritten eventually, but for now, it is important that it is reporting the condition reliably before it dies.

Thing is, creating an error message and submitting it into the logging framework itself needs stack space. Possibly considerable amounts of stack space; I had a noteworthy situation when the stack overflowed before all classes needed for logging were loaded, so another stack overflow happened inside the class loader, obscuring the original stack overflow.

Note that these crashes can and do happen in situation where it's desirable that the software continues to run. My current situation is unit tests, where some tests could overflow the stack (and in fact are expected to) but I still want to see the results of other tests. Another conceivable situation would be a server that hits a stack overflow; it should not need a restart just because one of several requests being processed ran into a stack overflow.

I can imagine various ways to deal with this, but is there a discussion of the various approaches, how they work in practice, and what JVM guarantees they can rely on? E.g. does the JVM guarantee some reserve stack space on StackOverflowError, and if yes, where's the specification?


Solution

  • Just let the StackOverflowError unwind the stack, and report it from the first nonrecursive function that is calling into the recursion. Thanks to user207421 for the idea, it's very clean and actually what I'd consider the go-to solution.

    If you don't control the code that calls into the recursion, wrap the recursive call into a new function that catches and reports the StackOverflowError.

    You'll often want to create a trace of values from each recursion level. It's going to be a very long exception message, and it's going to be complicated since you want to start reporting back as deep down in the stack as possible without overflowing the stack with your reporting machinery. I tried to write a recipe here, but it became too complicated, so it's going to be a library for Maven Central I guess.