mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +01:00
[jsscripting] Synchronize context access in logger initialization (#17496)
* [jsscripting] Synchronize context access in logger initialisation to avoid illegal multi-thread access Fixes #17494. Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
parent
82ba5da24c
commit
856d526d95
@ -15,6 +15,7 @@ package org.openhab.automation.jsscripting.internal;
|
||||
import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.script.Compilable;
|
||||
@ -34,7 +35,7 @@ import org.slf4j.LoggerFactory;
|
||||
* @author Jonathan Gilbert - Initial contribution
|
||||
* @author Florian Hotze - Improve logger name, Fix memory leak caused by exception logging
|
||||
*/
|
||||
class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable & Compilable>
|
||||
class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable & Compilable & Lock>
|
||||
extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable<T> {
|
||||
|
||||
private static final int STACK_TRACE_LENGTH = 5;
|
||||
@ -48,8 +49,18 @@ class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoClosea
|
||||
@Override
|
||||
protected void beforeInvocation() {
|
||||
super.beforeInvocation();
|
||||
if (logger == null) {
|
||||
initializeLogger();
|
||||
// OpenhabGraalJSScriptEngine::beforeInvocation will be executed after
|
||||
// DebuggingGraalScriptEngine::beforeInvocation, because GraalJSScriptEngineFactory::createScriptEngine returns
|
||||
// a DebuggingGraalScriptEngine instance.
|
||||
// We therefore need to synchronize logger setup here and cannot rely on the synchronization in
|
||||
// OpenhabGraalJSScriptEngine.
|
||||
delegate.lock();
|
||||
try {
|
||||
if (logger == null) {
|
||||
initializeLogger();
|
||||
}
|
||||
} finally { // Make sure that Lock is unlocked regardless of an exception being thrown or not to avoid deadlocks
|
||||
delegate.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,6 +32,8 @@ import java.time.Instant;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.Consumer;
|
||||
@ -69,7 +71,8 @@ import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
|
||||
* {@link Lock} for multi-thread synchronization; globals and openhab-js injection code caching
|
||||
*/
|
||||
public class OpenhabGraalJSScriptEngine
|
||||
extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable<GraalJSScriptEngine> {
|
||||
extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable<GraalJSScriptEngine>
|
||||
implements Lock {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(OpenhabGraalJSScriptEngine.class);
|
||||
private static final Source GLOBAL_SOURCE;
|
||||
@ -346,4 +349,34 @@ public class OpenhabGraalJSScriptEngine
|
||||
|
||||
return new InputStreamReader(ioStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lock() {
|
||||
lock.lock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lockInterruptibly() throws InterruptedException {
|
||||
lock.lockInterruptibly();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock() {
|
||||
return lock.tryLock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(long l, TimeUnit timeUnit) throws InterruptedException {
|
||||
return lock.tryLock(l, timeUnit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlock() {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Condition newCondition() {
|
||||
return lock.newCondition();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user