diff --git a/src/main/java/com/laytonsmith/core/constructs/CClosure.java b/src/main/java/com/laytonsmith/core/constructs/CClosure.java index efc7dc806..db0c16ebf 100644 --- a/src/main/java/com/laytonsmith/core/constructs/CClosure.java +++ b/src/main/java/com/laytonsmith/core/constructs/CClosure.java @@ -207,13 +207,18 @@ protected void execute(Mixed... values) throws ConfigRuntimeException, ProgramFl if(node == null) { return; } - StackTraceManager stManager = env.getEnv(GlobalEnv.class).GetStackTraceManager(); - stManager.addStackTraceElement(new ConfigRuntimeException.StackTraceElement("<>", getTarget())); + Environment environment; try { - Environment environment; synchronized(this) { environment = env.clone(); } + } catch (CloneNotSupportedException ex) { + Logger.getLogger(CClosure.class.getName()).log(Level.SEVERE, null, ex); + return; + } + StackTraceManager stManager = environment.getEnv(GlobalEnv.class).GetStackTraceManager(); + stManager.addStackTraceElement(new ConfigRuntimeException.StackTraceElement("<>", getTarget())); + try { CArray arguments = new CArray(node.getData().getTarget()); CArray vararg = null; CClassType varargType = null; diff --git a/src/main/java/com/laytonsmith/core/environments/GlobalEnv.java b/src/main/java/com/laytonsmith/core/environments/GlobalEnv.java index 7ad8e17bc..fcc2d79f6 100644 --- a/src/main/java/com/laytonsmith/core/environments/GlobalEnv.java +++ b/src/main/java/com/laytonsmith/core/environments/GlobalEnv.java @@ -27,7 +27,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; /** @@ -54,7 +53,8 @@ public class GlobalEnv implements Environment.EnvironmentImpl, Cloneable { private BoundEvent.ActiveEvent event = null; private boolean interrupt = false; private final List arrayAccessList = Collections.synchronizedList(new ArrayList<>()); - private final WeakHashMap stackTraceManagers = new WeakHashMap<>(); + private Thread stackTraceManagerThread = null; + private StackTraceManager stackTraceManager = null; private final MutableObject> runtimeSettings = new MutableObject<>(new ConcurrentHashMap<>()); private FileOptions fileOptions; @@ -155,6 +155,8 @@ public EnvironmentImpl clone() throws CloneNotSupportedException { } else if(!cloneVars) { clone.iVariableList = new IVariableList(clone.iVariableList); } + clone.stackTraceManager = this.stackTraceManager; + clone.stackTraceManagerThread = this.stackTraceManagerThread; return clone; } @@ -344,14 +346,11 @@ public List GetArrayAccessIteratorsFor(ArrayAccess array) { */ public StackTraceManager GetStackTraceManager() { Thread currentThread = Thread.currentThread(); - synchronized(stackTraceManagers) { - StackTraceManager manager = stackTraceManagers.get(currentThread); - if(manager == null) { - manager = new StackTraceManager(); - stackTraceManagers.put(currentThread, manager); - } - return manager; + if(this.stackTraceManager == null || currentThread != this.stackTraceManagerThread) { + this.stackTraceManager = new StackTraceManager(); + this.stackTraceManagerThread = currentThread; } + return this.stackTraceManager; } /**