Skip to content

fix for [MNT-23158] brakes context in nested JS calls #3477

@hi-ko

Description

@hi-ko

Since the changes done for MNT-23158, contexts in a running JavaSrcript are lost during execution when interacting with Java logic (behaviors, rules, actions, web scripts, js root scope objects) which also call a JavaScript.

It no longer possible to access the context in the outer/parent script after the java execution has run the child script. The scope of the parent script will be unset from the inner execution.

e.g. running following javascript

var node = document;
var scriptResponse = customExtension.executeScript('workspace://SpacesStore/0e85ce81-6dbd-4f7d-b58f-90dd8b53e205',{document: document, test1: "foo"})
logger.log(scriptResponse); // this runs successfull without issue and returns as expected

// after the script has been run by the customExtension the active script lost context!
node = utils.getNodeFromString("workspace://SpacesStore/08395317-00ab-4e77-88fa-33f6e95224fa");
logger.log(node); // this works

logger.log(node.name) // this will fail since the context is broken

will throw:

Cannot invoke "org.mozilla.javascript.Scriptable.getParentScope()" because "obj" is null
2025-07-15T10:07:20,119 [] ERROR [repo.action.AsynchronousActionExecutionQueueImpl] [defaultAsyncAction1] Failed to execute asynchronous action: Action[ id=1bfe7caf-b012-4604-92f4-d808aa9a5029, node=workspace://SpacesStore/1bfe7caf-b012-4604-92f4-d808aa9a5029 ]: Cannot invoke "org.mozilla.javascript.Scriptable.getParentScope()" because "obj" is null
org.alfresco.scripts.ScriptException: 06150046 Failed to execute script 'workspace://SpacesStore/a287d247-50da-401e-875c-afeedbcb452e': Cannot invoke "org.mozilla.javascript.Scriptable.getParentScope()" because "obj" is null
        at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:328) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.processor.ScriptServiceImpl.execute(ScriptServiceImpl.java:265) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:150) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:569) ~[?:?]
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.14.jar:6.0.14]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) ~[spring-aop-6.0.14.jar:6.0.14]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-6.0.14.jar:6.0.14]
        at org.alfresco.repo.security.permissions.impl.AlwaysProceedMethodInterceptor.invoke(AlwaysProceedMethodInterceptor.java:41) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.14.jar:6.0.14]
        at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:53) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.14.jar:6.0.14]
        at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:166) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.14.jar:6.0.14]
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-6.0.14.jar:6.0.14]
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:391) ~[spring-tx-6.0.14.jar:6.0.14]
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-6.0.14.jar:6.0.14]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.14.jar:6.0.14]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:244) ~[spring-aop-6.0.14.jar:6.0.14]
        at jdk.proxy4.$Proxy203.executeScript(Unknown Source) ~[?:?]
        at org.alfresco.repo.action.executer.ScriptActionExecuter.executeImpl(ScriptActionExecuter.java:178) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.action.executer.ActionExecuterAbstractBase.execute(ActionExecuterAbstractBase.java:293) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.action.ActionServiceImpl.directActionExecution(ActionServiceImpl.java:910) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.action.executer.CompositeActionExecuter.executeImpl(CompositeActionExecuter.java:82) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.action.executer.ActionExecuterAbstractBase.execute(ActionExecuterAbstractBase.java:293) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.action.ActionServiceImpl.directActionExecution(ActionServiceImpl.java:910) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.action.ActionServiceImpl.executeActionImpl(ActionServiceImpl.java:811) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.action.AsynchronousActionExecutionQueueImpl$ActionExecutionWrapper$1$1.execute(AsynchronousActionExecutionQueueImpl.java:433) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:452) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:321) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.action.AsynchronousActionExecutionQueueImpl$ActionExecutionWrapper$1.doWork(AsynchronousActionExecutionQueueImpl.java:442) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.tenant.TenantUtil.runAsWork(TenantUtil.java:126) ~[alfresco-data-model-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.tenant.TenantUtil.runAsTenant(TenantUtil.java:95) ~[alfresco-data-model-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.tenant.TenantUtil$1.doWork(TenantUtil.java:69) ~[alfresco-data-model-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.security.authentication.AuthenticationUtil.runAs(AuthenticationUtil.java:602) ~[alfresco-data-model-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.tenant.TenantUtil.runAsUserTenant(TenantUtil.java:65) ~[alfresco-data-model-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.action.AsynchronousActionExecutionQueueImpl$ActionExecutionWrapper.run(AsynchronousActionExecutionQueueImpl.java:445) [alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
        at java.lang.Thread.run(Thread.java:840) [?:?]
Caused by: java.lang.NullPointerException: Cannot invoke "org.mozilla.javascript.Scriptable.getParentScope()" because "obj" is null
        at org.mozilla.javascript.ScriptableObject.getTopLevelScope(ScriptableObject.java:1941) ~[rhino-1.7.14.jar:1.7.14]
        at org.mozilla.javascript.ScriptRuntime.setBuiltinProtoAndParent(ScriptRuntime.java:4271) ~[rhino-1.7.14.jar:1.7.14]
        at org.mozilla.javascript.Context.newArray(Context.java:1562) ~[rhino-1.7.14.jar:1.7.14]
        at org.alfresco.repo.jscript.ValueConverter.convertValueForScript(ValueConverter.java:129) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.jscript.ScriptNode$NodeValueConverter.convertValueForScript(ScriptNode.java:3815) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.jscript.ScriptNode$NodeValueConverter.convertValueForScript(ScriptNode.java:3794) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.jscript.ScriptNode.getProperties(ScriptNode.java:1008) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.jscript.ScriptNode.getName(ScriptNode.java:400) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at jdk.internal.reflect.GeneratedMethodAccessor590.invoke(Unknown Source) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:569) ~[?:?]
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:206) ~[rhino-1.7.14.jar:1.7.14]
        at org.mozilla.javascript.JavaMembers.get(JavaMembers.java:95) ~[rhino-1.7.14.jar:1.7.14]
        at org.mozilla.javascript.NativeJavaObject.get(NativeJavaObject.java:95) ~[rhino-1.7.14.jar:1.7.14]
        at org.alfresco.repo.jscript.SandboxNativeJavaObject.get(SandboxNativeJavaObject.java:49) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.mozilla.javascript.ScriptableObject.getProperty(ScriptableObject.java:2023) ~[rhino-1.7.14.jar:1.7.14]
        at org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1672) ~[rhino-1.7.14.jar:1.7.14]
        at org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1667) ~[rhino-1.7.14.jar:1.7.14]
        at org.mozilla.javascript.gen.workspace___SpacesStore_a287d247_50da_401e_875c_afeedbcb452e_54._c_script_0(workspace://SpacesStore/a287d247-50da-401e-875c-afeedbcb452e:209) ~[?:?]
        at org.mozilla.javascript.gen.workspace___SpacesStore_a287d247_50da_401e_875c_afeedbcb452e_54.call(workspace://SpacesStore/a287d247-50da-401e-875c-afeedbcb452e) ~[?:?]
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:380) ~[rhino-1.7.14.jar:1.7.14]
        at org.alfresco.repo.jscript.AlfrescoContextFactory.doTopCall(AlfrescoContextFactory.java:127) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3868) ~[rhino-1.7.14.jar:1.7.14]
        at org.mozilla.javascript.gen.workspace___SpacesStore_a287d247_50da_401e_875c_afeedbcb452e_54.call(workspace://SpacesStore/a287d247-50da-401e-875c-afeedbcb452e) ~[?:?]
        at org.mozilla.javascript.gen.workspace___SpacesStore_a287d247_50da_401e_875c_afeedbcb452e_54.exec(workspace://SpacesStore/a287d247-50da-401e-875c-afeedbcb452e) ~[?:?]
        at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:589) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:324) ~[alfresco-repository-23.2.0.60.jar:23.2.0.60]
        ... 41 more

while the parent script is still running, the customExtension runs another (nested) script as hook

scriptService.executeScript(nodeRef, customModel);

but as soon the nested script finishes and the customExtension returns, the context is broken for the parent script above / the scope not only from the nested but also from the parent script is unset. This means, it's still possible to work with customExtension's return but from the next command, utils.getNodeFromString() it's no longer possible to get it's context like node.name

This is totally unexpected and should be seen as a bug and is a braking change. Nested Java<->JS interop contexts should be seen as perfectly valid due to the interaction of behaviours, rules, actions, web scripts etc.

Instead only the context of the inner script should be unset and therefor not share any context to avoid memory issues like addressed in MNT-23158.

Caused by the changes done in [MNT-23158] Scripts limits configuration and optimization #1519

Metadata

Metadata

Assignees

No one assigned

    Labels

    externalExternal Contribution

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions