mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-26 15:21:41 +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> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
b695881824
commit
bd7cbed62b
@ -15,6 +15,7 @@ package org.openhab.automation.jsscripting.internal;
|
|||||||
import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT;
|
import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.script.Compilable;
|
import javax.script.Compilable;
|
||||||
@ -34,7 +35,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
* @author Jonathan Gilbert - Initial contribution
|
* @author Jonathan Gilbert - Initial contribution
|
||||||
* @author Florian Hotze - Improve logger name, Fix memory leak caused by exception logging
|
* @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> {
|
extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable<T> {
|
||||||
|
|
||||||
private static final int STACK_TRACE_LENGTH = 5;
|
private static final int STACK_TRACE_LENGTH = 5;
|
||||||
@ -48,8 +49,18 @@ class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoClosea
|
|||||||
@Override
|
@Override
|
||||||
protected void beforeInvocation() {
|
protected void beforeInvocation() {
|
||||||
super.beforeInvocation();
|
super.beforeInvocation();
|
||||||
if (logger == null) {
|
// OpenhabGraalJSScriptEngine::beforeInvocation will be executed after
|
||||||
initializeLogger();
|
// 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.time.ZonedDateTime;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
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.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.function.Consumer;
|
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
|
* {@link Lock} for multi-thread synchronization; globals and openhab-js injection code caching
|
||||||
*/
|
*/
|
||||||
public class OpenhabGraalJSScriptEngine
|
public class OpenhabGraalJSScriptEngine
|
||||||
extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable<GraalJSScriptEngine> {
|
extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable<GraalJSScriptEngine>
|
||||||
|
implements Lock {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(OpenhabGraalJSScriptEngine.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(OpenhabGraalJSScriptEngine.class);
|
||||||
private static final Source GLOBAL_SOURCE;
|
private static final Source GLOBAL_SOURCE;
|
||||||
@ -346,4 +349,34 @@ public class OpenhabGraalJSScriptEngine
|
|||||||
|
|
||||||
return new InputStreamReader(ioStream);
|
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