LiquidPlayer / LiquidCore

Node.js virtual machine for Android and iOS
MIT License
1.01k stars 128 forks source link

JSContext pointing to "null" object. #18

Closed ayushnawani closed 7 years ago

ayushnawani commented 7 years ago

Hi,

Scenario:

This happens with JSFunction and JSObject references also. Without any thread i am able to access the JSContext refence.

Thanks , Ayush

ericwlange commented 7 years ago

Do you have a simple failing test that I can debug? Or a few lines of code?

ayushnawani commented 7 years ago

I have written below code similar to my original code which throws error.

public class ContextTest {

    private Map<Integer,JSContext> contextMap = null;

    public ContextTest() {
        contextMap = new HashMap<>();
    }

    public void createContext(int id) {

        JSContext context = new JSContext();
        context.setExceptionHandler(new JSExceptionHandler());

        try {
            contextMap.put(id,context);
//            JSValue onMessage = context.property("onmessage");

            try {
                // onMessage.toFunction().call();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        catch (Exception ioe) {
            ioe.printStackTrace();
        }
    }

    public void callFunction(final int id) {

        new Thread() {
            @Override
            public void run() {
                JSContext context = contextMap.get(id);
//                JSValue onMessage = context.property("onmessage");
                System.out.println("THREAD TEST " + context);
//                onMessage.toFunction().call();
            }
        }.start();

    }

    public class JSExceptionHandler implements JSContext.IJSExceptionHandler {
        @Override
        public void handle(JSException exception) {
            exception.printStackTrace();
        }
    }

}

Calling from other class


          ContextTest contextTest = new ContextTest();
          contextTest.createContext(12);
          contextTest.callFunction(12);
ayushnawani commented 7 years ago

This is the android studio monitor report which is coming.

03-16 02:10:39.014 20776-20782/com.demo I/art: Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 2.718ms total 11.247ms
03-16 02:10:39.014 20776-20784/com.demo I/art: WaitForGcToComplete blocked for 88.702ms for cause Alloc
03-16 02:10:39.014 20776-20784/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.014 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.014 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.015 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.033 20776-20784/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.033 20776-20784/com.demo I/art: Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 3.768ms total 18.155ms
03-16 02:10:39.033 20776-20776/com.demo I/art: WaitForGcToComplete blocked for 66.520ms for cause Alloc
03-16 02:10:39.033 20776-20776/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.033 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.033 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.033 20776-20784/com.demo I/art: Forcing collection of SoftReferences for 28B allocation
03-16 02:10:39.033 20776-20784/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.044 20776-20776/com.demo I/art: Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 3.457ms total 11.144ms
03-16 02:10:39.044 20776-20776/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.044 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.044 20776-20784/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.044 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.064 20776-20776/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.064 20776-20776/com.demo I/art: Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 6.051ms total 19.851ms
03-16 02:10:39.065 20776-20782/com.demo I/art: WaitForGcToComplete blocked for 49.574ms for cause Alloc
03-16 02:10:39.065 20776-20782/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.065 20776-20784/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.065 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.065 20776-20776/com.demo I/art: Forcing collection of SoftReferences for 32B allocation
03-16 02:10:39.065 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.100 20776-20782/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.100 20776-20782/com.demo I/art: Alloc partial concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 9.853ms total 35.231ms
03-16 02:10:39.101 20776-20782/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.102 20776-20784/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.102 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.102 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.137 20776-20782/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.137 20776-20782/com.demo I/art: Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 13.882ms total 36.388ms
03-16 02:10:39.138 20776-20784/com.demo I/art: WaitForGcToComplete blocked for 104.620ms for cause Alloc
03-16 02:10:39.138 20776-20784/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.138 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.138 20776-20782/com.demo I/art: Forcing collection of SoftReferences for 13B allocation
03-16 02:10:39.139 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.139 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.177 20776-20784/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.177 20776-20784/com.demo I/art: Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 5.545ms total 39.280ms
03-16 02:10:39.178 20776-20784/com.demo W/art: Throwing OutOfMemoryError "Failed to allocate a 28 byte allocation with 3144 free bytes and 3KB until OOM" (recursive case)
03-16 02:10:39.179 20776-20784/com.demo W/art: "FinalizerDaemon" daemon prio=5 tid=5 Runnable
03-16 02:10:39.179 20776-20784/com.demo W/art:   | group="system" sCount=0 dsCount=0 obj=0x22c081c0 self=0xec9b5000
03-16 02:10:39.179 20776-20784/com.demo W/art:   | sysTid=20784 nice=0 cgrp=default sched=0/0 handle=0xf2f08930
03-16 02:10:39.179 20776-20784/com.demo W/art:   | state=R schedstat=( 550238694 21758581 417 ) utm=38 stm=16 core=2 HZ=100
03-16 02:10:39.179 20776-20784/com.demo W/art:   | stack=0xf2e06000-0xf2e08000 stackSize=1038KB
03-16 02:10:39.182 20776-20784/com.demo W/art:   | held mutexes= "mutator lock"(shared held)
03-16 02:10:39.182 20776-20784/com.demo W/art:     at com.android.internal.os.BinderInternal$GcWatcher.finalize(BinderInternal.java:53)
03-16 02:10:39.182 20776-20784/com.demo W/art:     at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:202)
03-16 02:10:39.182 20776-20784/com.demo W/art:     at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
03-16 02:10:39.182 20776-20784/com.demo W/art:     at java.lang.Thread.run(Thread.java:818)
03-16 02:10:39.183 20776-20861/com.demo I/art: WaitForGcToComplete blocked for 165.196ms for cause Alloc
03-16 02:10:39.183 20776-20861/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.183 20776-20784/com.demo E/System: Uncaught exception thrown by finalizer
03-16 02:10:39.183 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.184 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.185 20776-20784/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.200 20776-20861/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.200 20776-20861/com.demo I/art: Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 2.949ms total 17.049ms
03-16 02:10:39.200 20776-20782/com.demo I/art: WaitForGcToComplete blocked for 61.468ms for cause Alloc
03-16 02:10:39.200 20776-20782/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.200 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.200 20776-20784/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.201 20776-20861/com.demo I/art: Forcing collection of SoftReferences for 68B allocation
03-16 02:10:39.201 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.216 20776-20782/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.216 20776-20782/com.demo I/art: Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 3.081ms total 16.056ms
03-16 02:10:39.216 20776-20776/com.demo I/art: WaitForGcToComplete blocked for 150.976ms for cause Alloc
03-16 02:10:39.216 20776-20776/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.216 20776-20784/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.216 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.217 20776-20782/com.demo W/art: Throwing OutOfMemoryError "Failed to allocate a 13 byte allocation with 3144 free bytes and 3KB until OOM"
03-16 02:10:39.217 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.232 20776-20776/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.232 20776-20776/com.demo I/art: Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 2.924ms total 15.417ms
03-16 02:10:39.232 20776-20784/com.demo I/art: WaitForGcToComplete blocked for 47.052ms for cause Alloc
03-16 02:10:39.232 20776-20784/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.232 20776-20784/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.232 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.232 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.244 20776-20776/com.demo W/art: Suspending all threads took: 8.464ms
03-16 02:10:39.245 20776-20784/com.demo I/art: Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 2.877ms total 12.516ms
03-16 02:10:39.245 20776-20861/com.demo I/art: WaitForGcToComplete blocked for 44.019ms for cause Alloc
03-16 02:10:39.245 20776-20861/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.245 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.245 20776-20784/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.250 20776-20776/com.demo W/art: Throwing OutOfMemoryError "Failed to allocate a 32 byte allocation with 3144 free bytes and 3KB until OOM"
03-16 02:10:39.250 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.261 20776-20861/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.261 20776-20861/com.demo I/art: Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 3.111ms total 16.466ms
03-16 02:10:39.262 20776-20782/com.demo I/art: WaitForGcToComplete blocked for 44.824ms for cause Alloc
03-16 02:10:39.262 20776-20782/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.262 20776-20784/com.demo I/art: WaitForGcToComplete blocked for 16.277ms for cause Alloc
03-16 02:10:39.262 20776-20784/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.262 20776-20861/com.demo W/art: Throwing OutOfMemoryError "Failed to allocate a 68 byte allocation with 3144 free bytes and 3KB until OOM" (recursive case)
03-16 02:10:39.262 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.262 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.274 20776-20861/com.demo W/art: "AsyncTask #5" prio=5 tid=26 Runnable
03-16 02:10:39.274 20776-20861/com.demo W/art:   | group="main" sCount=0 dsCount=0 obj=0x22c08940 self=0xec9b8c00
03-16 02:10:39.274 20776-20861/com.demo W/art:   | sysTid=20861 nice=10 cgrp=default sched=0/0 handle=0xd609f930
03-16 02:10:39.274 20776-20861/com.demo W/art:   | state=R schedstat=( 4205779439 191794597 692 ) utm=392 stm=28 core=2 HZ=100
03-16 02:10:39.274 20776-20861/com.demo W/art:   | stack=0xd5f9d000-0xd5f9f000 stackSize=1038KB
03-16 02:10:39.274 20776-20861/com.demo W/art:   | held mutexes= "mutator lock"(shared held)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSException.<init>(JSException.java:56)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSObject.property(JSObject.java:316)
03-16 02:10:39.274 20776-20861/com.demo W/art:     at org.liquidplayer.javascript.JSContext.throwJSException(JSContext.java:207)
03-16 02:10:39.337 20776-20861/com.demo E/art: No exception class java.lang.OutOfMemoryError
03-16 02:10:39.340 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.351 20776-20784/com.demo I/art: Clamp target GC heap from 111MB to 96MB
03-16 02:10:39.351 20776-20784/com.demo I/art: Alloc partial concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 833us total 88.797ms
03-16 02:10:39.351 20776-20784/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.353 20776-20782/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.354 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.360 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.488 20776-20784/com.demo I/art: Alloc concurrent mark sweep GC freed 14159(6MB) AllocSpace objects, 2575(85MB) LOS objects, 40% free, 3MB/6MB, paused 362us total 135.106ms
03-16 02:10:39.489 20776-20782/com.demo I/art: WaitForGcToComplete blocked for 226.604ms for cause Alloc
03-16 02:10:39.489 20776-20782/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.489 20776-20776/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.489 20776-20861/com.demo I/art: Waiting for a blocking GC Alloc
03-16 02:10:39.492 20776-20784/com.demo E/System: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
03-16 02:10:39.495 20776-20782/com.demo I/art: Alloc sticky concurrent mark sweep GC freed 178(4KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 3MB/6MB, paused 253us total 5.857ms
03-16 02:10:39.495 20776-20776/com.demo I/art: WaitForGcToComplete blocked for 244.669ms for cause Alloc
03-16 02:10:39.495 20776-20776/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.495 20776-20861/com.demo I/art: WaitForGcToComplete blocked for 154.610ms for cause Alloc
03-16 02:10:39.495 20776-20861/com.demo I/art: Starting a blocking GC Alloc
03-16 02:10:39.495 20776-20782/com.demo W/art: byte[] allocation failed: 1
03-16 02:10:39.495 20776-20782/com.demo A/art: art/runtime/jdwp/jdwp_request.cc:48] Check failed: p_ == end_ read too few bytes: 1
03-16 02:10:39.496 20776-20776/com.demo D/AndroidRuntime: Shutting down VM

                                                                --------- beginning of crash
03-16 02:10:39.496 20776-20776/com.demo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.demo, PID: 20776
                                                                java.lang.OutOfMemoryError: Failed to allocate a 32 byte allocation with 3144 free bytes and 3KB until OOM
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
ericwlange commented 7 years ago

Let me try to reproduce.

ericwlange commented 7 years ago

Yup definitely a bug. Digging in ...

ericwlange commented 7 years ago

Fixed in d2370c1079a7563f18e6482d3a2a58218b88127d

ericwlange commented 7 years ago

Please try version 0.2.1. This should be fixed now.

ayushnawani commented 7 years ago

Hi Eric, Thank you for solving this.

For the above scenario its working now but when i am using ExecutorService, then JSContext is still pointing to "null" reference inside runnable.

Below is the changed callFunction of the above class ContextTest.

    public void callFunction(final int id) {

        ExecutorService service = Executors.newSingleThreadExecutor();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                JSContext context = contextMap.get(id);
                // JSValue onMessage = context.property("onmessage");
                // onMessage.toFunction().call();
                Log.d("Thread", "context : " + context.toString());
            }
        };
        service.execute(runnable);
}

From other Class i am calling like this :

    ContextTest contextTest = new ContextTest();
    contextTest.createContext(12);
    for(int i =0; i < 10; i++) {
      contextTest.callFunction(12);
    }

Output Trace :

03-22 07:36:54.430 22074-22182/com.demo W/System.err: Unknown Error
03-22 07:36:54.430 22074-22182/com.demo W/System.err:     at org.liquidplayer.javascript.JSValue.toString(JSValue.java:437)
03-22 07:36:54.430 22074-22182/com.demo W/System.err:     at com.metricstream.demo.javaScriptExecutors.ContextTest$1.run(ContextTest.java:75)
03-22 07:36:54.430 22074-22182/com.demo W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
03-22 07:36:54.430 22074-22182/com.demo W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
03-22 07:36:54.430 22074-22182/com.demo W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-22 07:36:54.430 22074-22182/com.demo D/Thread: callFunction null

Thanks, Ayush Nawani

ericwlange commented 7 years ago

I learned something today.

The issue is not with LiquidCore. Apparently HashMap is not thread-safe. See this StackOverflow discussion. You need to change your constructor and create some synchronization object:

    private Map<Integer,JSContext> contextMap;
    private final Object mutex = new Object();

    public ContextTest() {
        contextMap = Collections.synchronizedMap(new HashMap<Integer,JSContext>());
    }

And then wrap all attempts to access contextMap in a synchronized(mutex) {...} block.

I tested this and it works as expected.

ayushnawani commented 7 years ago

its working, thanks for helping me.