From 7de57f9ffe4eb5057d8c0bf6e78c538f54f69e73 Mon Sep 17 00:00:00 2001 From: Mark Hilbush Date: Sat, 20 Feb 2021 13:53:44 -0500 Subject: [PATCH] [automation] Fixed memory leak caused by invalid UI DSL rule (#2212) Signed-off-by: Mark Hilbush --- .../runtime/internal/engine/ScriptEngineImpl.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.core.model.script.runtime/src/org/openhab/core/model/script/runtime/internal/engine/ScriptEngineImpl.java b/bundles/org.openhab.core.model.script.runtime/src/org/openhab/core/model/script/runtime/internal/engine/ScriptEngineImpl.java index 2562965c8..054f39673 100644 --- a/bundles/org.openhab.core.model.script.runtime/src/org/openhab/core/model/script/runtime/internal/engine/ScriptEngineImpl.java +++ b/bundles/org.openhab.core.model.script.runtime/src/org/openhab/core/model/script/runtime/internal/engine/ScriptEngineImpl.java @@ -14,6 +14,7 @@ package org.openhab.core.model.script.runtime.internal.engine; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -136,21 +137,23 @@ public class ScriptEngineImpl implements ScriptEngine, ModelParser { List errors = resource.getErrors(); if (!errors.isEmpty()) { + deleteResource(resource); throw new ScriptParsingException("Failed to parse expression (due to managed SyntaxError/s)", scriptAsString).addDiagnosticErrors(errors); } EList contents = resource.getContents(); - if (!contents.isEmpty()) { Iterable validationErrors = getValidationErrors(contents.get(0)); if (!validationErrors.iterator().hasNext()) { return (XExpression) contents.get(0); } else { + deleteResource(resource); throw new ScriptParsingException("Failed to parse expression (due to managed ValidationError/s)", scriptAsString).addValidationIssues(validationErrors); } } else { + deleteResource(resource); return null; } } @@ -184,4 +187,11 @@ public class ScriptEngineImpl implements ScriptEngine, ModelParser { return "script"; } + private void deleteResource(Resource resource) { + try { + resource.delete(Collections.emptyMap()); + } catch(IOException e) { + // Do nothing + } + } }