Chaos Engineering – Stackoverflow Error

In the series of chaos engineering articles, we have been learning to simulate various performance problems. In this post, let’s discuss how to simulate the ‘StackOverflow errors. StackOverflow error is a runtime error, which is thrown when a thread’s stack size exceeds its allocated memory limit.

Sample Program

Here is a sample program from the open source BuggyApp application, which generates the StackOverflowError.

package com.buggyapp.stackoverflow;

public class StackOverflowDemo {

   public void start() {
				
      start();
   }
}

You can notice the sample program contains the ‘StackOverflowDemo’ class. This class has a start() method which calls itself recursively. This implementation will cause the start() method to be invoked an infinite number of times. 

Fig: start() method repeatedly added to thread’s stack, resulting in StackOverflowError’

As per the implementation, the start() method will be added to the thread’s stack frame an infinite number of times. Thus, after a few thousand iterations thread’s stack size limit would be exceeded. Once the stack size limit is exceeded it will result in ‘StackOverflowError’.

Execution

When we executed above program, as expected ‘java.lang.StackOverflowError’ was thrown in seconds:

java.lang.StackOverflowError
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)
        at com.buggyapp.stackoverflow.StackOverflowDemo.start(StackOverflowDemo.java:33)

How to diagnose ‘java.lang.StackOverflowError’?

You can diagnose StackOverflowError either through a manual or automated approach. 

Manual approach

When an application experiences ‘StackOverflowError’ it will be either printed in the application log file or in a standard error stream. From the stacktrace you will be able to figure which line of code causing the infinite looping.

Automated approach

On the other hand, you can also use yCrash open source script, which would capture 360-degree data (GC log, 3 snapshots of thread dump, heap dump, netstat, iostat, vmstat, top, top -H,…) from your application stack within a minute and generate a bundle zip file. You can then either manually analyze these artifacts or upload it to yCrash server for automated analysis. We used the Automated approach. When we uploaded the captured artifacts to the server, it instantly generated the below root cause analysis report highlighting the source of the problem. 

Fig: yCrash highlighting thread may result in StackOverflowError

You can notice the yCrash tool precisely pointing out the thread stack length is greater than 200 lines and it has potential to generate StackOverflowError. The Tool also points out the stack trace of the thread which is going on an infinite loop. Using this information from the report, one can easily go ahead and fix the problematic code.

Leave a Reply

Powered by WordPress.com.

Up ↑

%d