Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jsscripting] Fix memory leak on script execution failure #16578

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts.
*
* @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>
extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T> {
Expand All @@ -49,11 +50,13 @@ protected void beforeInvocation() {
@Override
public Exception afterThrowsInvocation(Exception e) {
Throwable cause = e.getCause();
// OPS4J Pax Logging holds a reference to the exception, which causes the OpenhabGraalJSScriptEngine to not be
// removed from heap by garbage collection and causing a memory leak.
// Therefore, don't pass the exceptions itself to the logger, but only their message!
if (cause instanceof IllegalArgumentException) {
logger.error("Failed to execute script:", e);
}
if (cause instanceof PolyglotException) {
logger.error("Failed to execute script:", cause);
logger.error("Failed to execute script: {}", e.getMessage());
} else if (cause instanceof PolyglotException) {
logger.error("Failed to execute script: {}", cause.getMessage());
florian-h05 marked this conversation as resolved.
Show resolved Hide resolved
}
return e;
}
Expand Down