Repository navigation
[NAE-2449] ActionDelegate memory leak causes OOM - #452
Conversation
Refactor action handling logic and improve memory management in FieldActionsRunner and related classes.
|
Warning Review limit reached
More reviews will be available in 31 minutes and 37 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughIntroduces ChangesExpando-Based Action Dispatch and Cleanup
Sequence Diagram(s)sequenceDiagram
participant Script as Action Script
participant FAR as FieldActionsRunner
participant AD as ActionDelegate
participant FE as FunctionExpando
participant CSM as ClosureSignatureMatcher
Script->>FAR: run(action, useCase, task)
FAR->>FAR: getActionCode() — assign functions as properties, wrap globals in FunctionExpando
FAR->>AD: setProperty(name, closure) [accumulate overloads]
FAR->>AD: prepareCode(closure, delegate)
FAR->>AD: closure.call()
rect rgba(200, 150, 100, 0.5)
Note over FAR: Action invokes function
Script->>FE: functionName(args)
FE->>AD: super.methodMissing
AD->>CSM: matches(closure, args)
CSM-->>AD: true/false
alt match found
AD-->>Script: execute matched closure
else no match
FE->>AD: invokeMethod fallback
AD-->>Script: result
end
end
Note over FAR: finally block
FAR->>AD: clearAfterExecution()
FAR->>FAR: clearGroovyMetaClass(delegate, functionExpando)
FAR-->>Script: outcomes returned
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Introduce FunctionExpando as a lightweight proxy for scoped global functions, delegating property and method access to the parent ActionDelegate while rehydrating cached function closures only when invoked. Replace eager global function rehydration in FieldActionsRunner with lazy FunctionExpando-based binding to reduce heap usage during action preparation.
…cher to support dynamic method invocation and improve closure signature matching.
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@application-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/expando/FunctionExpando.groovy`:
- Around line 117-122: The methodMissing function in FunctionExpando catches all
MissingMethodException instances from super.methodMissing(), but this
incorrectly catches exceptions thrown from inside the closure body when it calls
undefined methods on parentDelegate. The catch block should only handle the case
where the method itself is not found in the parent, not exceptions occurring
during closure execution. Restructure the exception handling to distinguish
between "method not found" failures versus "method found but raised exception
during execution" scenarios, so that MissingMethodExceptions originating from
within the executed closure are allowed to propagate rather than being caught
and rerouted to parentDelegate.invokeMethod().
In
`@application-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/FieldActionsRunner.groovy`:
- Around line 68-70: In the FieldActionsRunner.groovy finally block where
outcomes are captured at line 69, add a null check before dereferencing
code.delegate.outcomes to guard against NPE. Check that both code.delegate
exists and that outcomes have been allocated before assigning them to the
ArrayList, ensuring that if code.init(...) fails before outcomes are
initialized, the finally block safely handles this case without throwing a
NullPointerException that would mask the original exception. This protects the
exception handling flow in the overall try-finally structure.
In
`@application-engine/src/main/java/com/netgrif/application/engine/event/GroovyShellFactory.java`:
- Around line 26-35: The lazy initialization of the shell field in
GroovyShellFactory lacks thread-safety guarantees. To fix this, declare the
shell field as volatile to ensure proper memory visibility across threads, and
wrap the entire null-check and initialization block (lines 26-35) in a
synchronized block using a lock object (typically synchronized on this or a
dedicated lock). This prevents multiple threads from simultaneously entering the
initialization block, which would otherwise cause duplicate ImportCustomizer
additions to configuration and non-deterministic shell publication across memory
barriers.
In
`@application-engine/src/main/java/com/netgrif/application/engine/utils/ClosureSignatureMatcher.java`:
- Around line 49-51: In the ClosureSignatureMatcher class, the loop iterating
through params needs to handle the case where params[i] is a primitive type.
Before calling isAssignableFrom on params[i], add logic to check if the
parameter is a primitive type, and if so, wrap it with its boxed equivalent
(e.g., int becomes Integer, long becomes Long). Additionally, when a parameter
is primitive and the corresponding args[i] is null, mark the match as false
immediately since primitives cannot accept null values. This ensures that when a
Groovy closure expects primitive parameters but receives boxed arguments at
runtime, the assignability check correctly evaluates to true, allowing valid
closure overloads to be properly matched.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 3285a8ed-1bf2-4b9a-8ebd-742995b92594
📒 Files selected for processing (6)
application-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/ActionDelegate.groovyapplication-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/FieldActionsRunner.groovyapplication-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/expando/DelegateExpando.groovyapplication-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/expando/FunctionExpando.groovyapplication-engine/src/main/java/com/netgrif/application/engine/event/GroovyShellFactory.javaapplication-engine/src/main/java/com/netgrif/application/engine/utils/ClosureSignatureMatcher.java
…fety; improve error handling in FunctionExpando; enhance memory management in FieldActionsRunner; and add primitive type wrapping to ClosureSignatureMatcher.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (3)
application-engine/src/main/java/com/netgrif/application/engine/utils/ClosureSignatureMatcher.java (2)
39-39:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winUpdate
@paramjavadoc to reflect null rejection for primitive parameters.The
@paramdocumentation states "null values in the array are considered compatible with any parameter type", but the implementation correctly rejects null when the corresponding parameter is primitive.📝 Proposed javadoc fix
* `@param` args the array of arguments to match against the closure's signature; - * null values in the array are considered compatible with any parameter type + * null values are compatible with reference parameter types but not with primitive parameter types * `@return` {`@code` true} if at least one {`@code` doCall} method in the closure matches the provided arguments;🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@application-engine/src/main/java/com/netgrif/application/engine/utils/ClosureSignatureMatcher.java` at line 39, Update the `@param` javadoc documentation in the ClosureSignatureMatcher class to accurately reflect the null handling behavior. The current documentation incorrectly states that null values are compatible with any parameter type, but the implementation actually rejects null values when the corresponding parameter is primitive. Revise the javadoc to clarify that null values are only compatible with non-primitive (object) parameter types, and that null is rejected for primitive parameters.
33-33:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winUpdate javadoc to reflect null rejection for primitive parameters.
The documentation states "Null arguments are allowed for any parameter type", but the implementation (lines 52-54) correctly rejects null arguments when the corresponding parameter is primitive. Primitive types cannot accept null values in Java.
📝 Proposed javadoc fix
* <li>The parameter count matches the argument count</li> * <li>Each argument's type is assignable to the corresponding parameter type</li> - * <li>Null arguments are allowed for any parameter type</li> + * <li>Null arguments are allowed for reference parameter types but rejected for primitive parameter types</li> * </ul>🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@application-engine/src/main/java/com/netgrif/application/engine/utils/ClosureSignatureMatcher.java` at line 33, The javadoc comment in ClosureSignatureMatcher.java at line 33 incorrectly states that null arguments are allowed for any parameter type, but the actual implementation in the validation logic (lines 52-54) correctly rejects null arguments for primitive parameters since Java primitives cannot be null. Update the javadoc comment to clarify that null arguments are allowed only for non-primitive parameter types, while primitive parameter types do not accept null values.application-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/FieldActionsRunner.groovy (1)
131-134:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winAdd defensive guard consistent with finally block.
cleanUpunconditionally castscode.delegatetoActionDelegate(line 132), but the finally block at lines 69-70 defensively checkscode?.delegate instanceof ActionDelegatebefore accessing it. Ifcode.delegateis unexpectedly null or not anActionDelegate, this throws an exception that masks the original error.🛡️ Proposed fix for consistency
private void cleanUp(Closure code) { - ((ActionDelegate) code.delegate).clearAfterExecution() + if (code?.delegate instanceof ActionDelegate) { + ((ActionDelegate) code.delegate).clearAfterExecution() + } clearGroovyMetaClass(code) }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@application-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/FieldActionsRunner.groovy` around lines 131 - 134, The cleanUp method unconditionally casts code.delegate to ActionDelegate without defensive checking, which is inconsistent with the finally block that defensively checks code?.delegate instanceof ActionDelegate before accessing it. Add a guard condition in the cleanUp method before calling clearAfterExecution() on the cast delegate, checking if code and code.delegate exist and that code.delegate is actually an instance of ActionDelegate, similar to the pattern used in the finally block. Only execute the clearAfterExecution() call when this condition is satisfied, ensuring defensive consistency and preventing exceptions that could mask original errors.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In
`@application-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/FieldActionsRunner.groovy`:
- Around line 131-134: The cleanUp method unconditionally casts code.delegate to
ActionDelegate without defensive checking, which is inconsistent with the
finally block that defensively checks code?.delegate instanceof ActionDelegate
before accessing it. Add a guard condition in the cleanUp method before calling
clearAfterExecution() on the cast delegate, checking if code and code.delegate
exist and that code.delegate is actually an instance of ActionDelegate, similar
to the pattern used in the finally block. Only execute the clearAfterExecution()
call when this condition is satisfied, ensuring defensive consistency and
preventing exceptions that could mask original errors.
In
`@application-engine/src/main/java/com/netgrif/application/engine/utils/ClosureSignatureMatcher.java`:
- Line 39: Update the `@param` javadoc documentation in the
ClosureSignatureMatcher class to accurately reflect the null handling behavior.
The current documentation incorrectly states that null values are compatible
with any parameter type, but the implementation actually rejects null values
when the corresponding parameter is primitive. Revise the javadoc to clarify
that null values are only compatible with non-primitive (object) parameter
types, and that null is rejected for primitive parameters.
- Line 33: The javadoc comment in ClosureSignatureMatcher.java at line 33
incorrectly states that null arguments are allowed for any parameter type, but
the actual implementation in the validation logic (lines 52-54) correctly
rejects null arguments for primitive parameters since Java primitives cannot be
null. Update the javadoc comment to clarify that null arguments are allowed only
for non-primitive parameter types, while primitive parameter types do not accept
null values.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 904a3215-dce9-4a6b-9978-fbf0145a11b7
📒 Files selected for processing (4)
application-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/FieldActionsRunner.groovyapplication-engine/src/main/groovy/com/netgrif/application/engine/petrinet/domain/dataset/logic/action/expando/FunctionExpando.groovyapplication-engine/src/main/java/com/netgrif/application/engine/event/GroovyShellFactory.javaapplication-engine/src/main/java/com/netgrif/application/engine/utils/ClosureSignatureMatcher.java
…nd log warning for invalid delegate type
56ec1f7
… Groovy metaClass.
Description
Refactor action handling logic and improve memory management in FieldActionsRunner and related classes.
Fixes NAE-2449
Dependencies
No new dependencies were introduced
Third party dependencies
No new dependencies were introduced
Blocking Pull requests
There are no dependencies on other PR
How Has Been This Tested?
This was tested manually and with unit tests.
Test Configuration
Checklist:
Summary by CodeRabbit