Commit 953054fadc8d25ddfddb500a355eaf3e70afa648

Authored by VoBa
Committed by GitHub
1 parent 6789cff8

eval is not thread safe. Added lock to avoid 'No such function invokeInternalXXX' exception (#4211)

* eval is not thread safe. Added lock to avoid 'No such function invokeInternalXXX' exception

* License fix
... ... @@ -40,6 +40,7 @@ import java.util.concurrent.ExecutorService;
40 40 import java.util.concurrent.Executors;
41 41 import java.util.concurrent.TimeUnit;
42 42 import java.util.concurrent.atomic.AtomicInteger;
  43 +import java.util.concurrent.locks.ReentrantLock;
43 44
44 45 @Slf4j
45 46 public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeService {
... ... @@ -56,6 +57,8 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer
56 57 private final FutureCallback<UUID> evalCallback = new JsStatCallback<>(jsEvalMsgs, jsTimeoutMsgs, jsFailedMsgs);
57 58 private final FutureCallback<Object> invokeCallback = new JsStatCallback<>(jsInvokeMsgs, jsTimeoutMsgs, jsFailedMsgs);
58 59
  60 + private final ReentrantLock evalLock = new ReentrantLock();
  61 +
59 62 @Getter
60 63 private final JsExecutorService jsExecutor;
61 64
... ... @@ -121,10 +124,15 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer
121 124 jsPushedMsgs.incrementAndGet();
122 125 ListenableFuture<UUID> result = jsExecutor.executeAsync(() -> {
123 126 try {
124   - if (useJsSandbox()) {
125   - sandbox.eval(jsScript);
126   - } else {
127   - engine.eval(jsScript);
  127 + evalLock.lock();
  128 + try {
  129 + if (useJsSandbox()) {
  130 + sandbox.eval(jsScript);
  131 + } else {
  132 + engine.eval(jsScript);
  133 + }
  134 + } finally {
  135 + evalLock.unlock();
128 136 }
129 137 scriptIdToNameMap.put(scriptId, functionName);
130 138 return scriptId;
... ...