In my last post I talked about ‘flusher threads’ which constantly ‘flush’ the recorded data from the memory buffer and persist it to disk.
However, due to the latencies in IO between memory and hard disk, it is entirely possible that your program terminates while there is still some data left in the memory buffer which hasn’t been persisted.
To solve this issue, we register a ‘shutdown hook’ with the jvm which essentially keeps your program alive for a little while even after it terminates, so that we can persist all the leftover data. That is why you may see messages like these on your console when your program shuts down when its running with Chronon.
However using the shutdown hook opens a whole can of worms of its own.
Since the jvm does not impose any sort of ordering of how shutdown hooks are run, we essentially ‘stop’ recording at this point. Thus if you have any custom shutdown hooks of your own, those will not be recorded.
There are also issues where the shutdown hook does not run, in which case the recording would be considered corrupt.
Some of the cases when the shutdown hook will run are :
- Program finishes execution normally.
- Program calls System.exit().
- Ctrl+C is used to kill the program.
- An uncaught exception terminates the program.
- The unix ‘kill’ command is used to terminate the program.
Cases when the shutdown hook does not run, thus leaving the recording in an invalid state :
- Program calls Runtime.halt().
- Unix command ‘kill -9′ is used to terminate the program.
- The ‘End Process’ option of the Windows Task Manager is used to terminate the program.
- Internal JVM crashes or crashes inside the native code.
- Any other program which sends a SIGKILL signal on an Unix machine or the TerminateProcess call on a Windows machine.
That said, most of our used will be using eclipse to launch and terminate their programs, so I really focused on making that use case always produce a valid recording.
Eclipse by itself unfortunately doesn’t seem to be of any help in this case since the default red ‘terminate’ button sends a SIGKILL signal thus terminating the JVM instantly without waiting for any shutdown hooks to run. Thus if you use the ‘red’ button to terminate your programs running with Chronon, the recording will always be invalid.
So I went ahead and added a ‘blue’ button next to the ‘red’ terminate button.
Pushing the ‘blue’ button will make sure the shutdown hook runs and you always get a valid recording. The blue button is active only when a program is running with Chronon enabled and it is what you should always use to stop your programs from within Eclipse.
You can still use the ‘red’ button when say you were just experimenting with something and no bugs appeared or for some other reason you just don’t care about the recording, but we recommend making the blue button your default for stopping programs from now on.