Search code examples
androidv8embedded-v8j2v8

Can I kill JavaScript execution in J2V8?


I've embedded v8 in my Android project via J2V8. This works great, but there are situations where the script hangs (probably due to some kind of endless loop I suppose). The code is way too complicated to debug it and there can always be a situation where a script is running mad.

To be sure not to have some zombie thread hanging around and draining battery, I'd like to define a timeout and kill v8 if the script has not yet returned. Of course I'm executing the script in AsyncTask or IntentService and never in the main thread. This way Android doesn't protect itself showing an "Application Not Responding" (ANR). The background thread runs and runs and runs...

How to kill v8 in a clean way?


Solution

  • I've overlooked this method on V8 object:

    /**
     * Terminates any JavaScript executing on this runtime. Once
     * the runtime is released, any executors that were spawned
     * will also be force terminated.
     */
    public void terminateExecution() {
        forceTerminateExecutors = true;
        terminateExecution(v8RuntimePtr);
    }