Cleanup code in SimpleApplication - On Crash

Hi Guys,
I have noticed that if my SimpleApplication based app crashes, it doesn’t removed from the task manager (Windows) which means the process still exists.
It happened recently when I have added some background threads (thread pool) for loading assets.
My guess (maybe I’m wrong, not sure about it) is that the threads are not cleaned properly due to the crash so it prevents the process from shutting down.
My question is how / where to perform the cleanup code in SimpleApplication so it will handle app crashes as well…


1 Like

Sounds to me like you first need to figure out what stays alive. VisualVM to the rescue. Take a thread dump or just look at the alive threads.

If it is the threads… panic solution is always to play with daemon (you can do it with pools too). I suspect your threads sleep forever or something like that?

1 Like

I’m not sure what do you mean by “sleep forever”. The threads are contained & maintained using ExecutorService:

private ExecutorService executorService = null;

Whenever needed, I fetch a thread from the pool and use it for asset loading.
One more option - I know that SimpleApplication shows a modal dialog on app crash, maybe it pops up but stays invisible (for some reason) preventing the process from shutting down…

Then you can probably tweak the configuration of your thread pool to work around it. Since you never call shutdown on it. It might just idle in the background forever. So unless you find a way to hook to a crash… You can tweak it to have 0 core threads and cool down time somewhere reasonable…

But also when looking at the code. I’m pretty sure you might be able to deal with this by overriding handleError on your SimpleApplication.


It depends on how the app crashes… but I do my thread pool shutdowns in an app state cleanup and they seem to get called under all of the cases where my app has crashed.

Where are you shutting your thread pools down now?

1 Like

I was shutting it when the scripting interpreter finished it’s job. I have added one more shutdown in the handleError override as @tonihele suggested and it should be fine now. Thanks!

But then how does it shutdown if the user exits the application?

If you put it in an app state cleanup then it will run when the user closes the program and it will run when handleError is called.

OK I understand. will modify the code to shutdown the threads in an app state. 10X

In case it’s relevant at all, note that SiO2 has a JobState that manages a worker pool:

…a Job is like a Runnable that will be run on the background thread and then run again on the render thread. Useful for doing something in the background and then interacting with JME.