From 07f1f781e16e21ddb13b4defe7ab5229c3b33430 Mon Sep 17 00:00:00 2001 From: Alexander Kell Date: Sun, 17 Mar 2024 18:34:09 +0700 Subject: [PATCH] improve graalvm multi thread errors with reason --- .../benerator/script/GraalScript.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/rapiddweller/benerator/script/GraalScript.java b/src/main/java/com/rapiddweller/benerator/script/GraalScript.java index 3871d554f..0a5a4317e 100644 --- a/src/main/java/com/rapiddweller/benerator/script/GraalScript.java +++ b/src/main/java/com/rapiddweller/benerator/script/GraalScript.java @@ -56,10 +56,20 @@ public GraalScript(String text, Engine scriptEngine, String languageId) { } @Override - public Object evaluate(Context context) throws ScriptException { - Value returnValue = globalPolyglotCtx.evalScript(context, text, language); - GraalValueConverter converter = new GraalValueConverter(); - return converter.convert(returnValue); + public synchronized Object evaluate(Context context) throws ScriptException { + try { + Value returnValue = globalPolyglotCtx.evalScript(context, text, language); + GraalValueConverter converter = new GraalValueConverter(); + return converter.convert(returnValue); + } catch (IllegalStateException e) { + if(e.getMessage().contains("Multi threaded access requested")) { + throw new ScriptException(String.format("Multi thread access requested. GraalVM Context is not thread safe. " + + "Please check your script and make sure there is no multi thread access with '%s'", text), null); + } + else { + throw new ScriptException("Error evaluating script: " + text, e); + } + } } @Override