Skip to content

GROOVY-12276: Extend the closure cycle check to the closures a wrappe… - #2813

Open
paulk-asert wants to merge 1 commit into
apache:masterfrom
paulk-asert:groovy12276
Open

GROOVY-12276: Extend the closure cycle check to the closures a wrappe…#2813
paulk-asert wants to merge 1 commit into
apache:masterfrom
paulk-asert:groovy12276

Conversation

@paulk-asert

Copy link
Copy Markdown
Contributor

…r calls

Closure.checkForReferenceCycle walks owner, delegate and thisObject, which are the fields a closure dispatches through. A closure which wraps another and invokes it recurses through its own field instead, so a forged graph cycling there passed the check and then exhausted the stack on invocation.

Measured before the change, building each cycle by reflection, serializing and reading it back:

CurriedClosure, cycle via owner rejected, InvalidObjectException
ComposedClosure, cycle via first/second accepted, then StackOverflowError
TrampolineClosure, cycle via original accepted, then StackOverflowError

ComposedClosure was the more surprising of the two: it already opts into the check from its readResolve, so the check ran and had nothing to say about the closure's own recursion. TrampolineClosure had no readResolve at all.

Add Closure.additionalReferences for a subclass to declare the closures it calls through, and walk those as well. ComposedClosure declares first and second; TrampolineClosure declares original and gains the readResolve it was missing.

Only fields a closure calls belong there, which is why the walk is not simply made reflective over every Closure-valued field: a captured closure is not a recursion edge, and treating it as one would reject graphs that invoke perfectly well.

The existing ComposedClosure cycle test forges its cycle through owner and delegate, so it exercised the walk that already worked; the new tests forge through the wrapped fields, and fail without this change.

…r calls

Closure.checkForReferenceCycle walks owner, delegate and thisObject, which
are the fields a closure dispatches through. A closure which wraps another
and invokes it recurses through its own field instead, so a forged graph
cycling there passed the check and then exhausted the stack on invocation.

Measured before the change, building each cycle by reflection, serializing
and reading it back:

  CurriedClosure, cycle via owner        rejected, InvalidObjectException
  ComposedClosure, cycle via first/second   accepted, then StackOverflowError
  TrampolineClosure, cycle via original     accepted, then StackOverflowError

ComposedClosure was the more surprising of the two: it already opts into the
check from its readResolve, so the check ran and had nothing to say about
the closure's own recursion. TrampolineClosure had no readResolve at all.

Add Closure.additionalReferences for a subclass to declare the closures it
calls through, and walk those as well. ComposedClosure declares first and
second; TrampolineClosure declares original and gains the readResolve it was
missing.

Only fields a closure calls belong there, which is why the walk is not
simply made reflective over every Closure-valued field: a captured closure
is not a recursion edge, and treating it as one would reject graphs that
invoke perfectly well.

The existing ComposedClosure cycle test forges its cycle through owner and
delegate, so it exercised the walk that already worked; the new tests forge
through the wrapped fields, and fail without this change.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.1856%. Comparing base (f140841) to head (99aff08).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #2813        +/-   ##
==================================================
+ Coverage     70.1837%   70.1856%   +0.0019%     
- Complexity      35844      35853         +9     
==================================================
  Files            1562       1562                
  Lines          132528     132540        +12     
  Branches        24379      24379                
==================================================
+ Hits            93013      93024        +11     
- Misses          31106      31109         +3     
+ Partials         8409       8407         -2     
Files with missing lines Coverage Δ
src/main/java/groovy/lang/Closure.java 85.0550% <100.0000%> (+0.2675%) ⬆️
src/main/java/groovy/lang/TrampolineClosure.java 76.4706% <100.0000%> (+5.0420%) ⬆️
...a/org/codehaus/groovy/runtime/ComposedClosure.java 77.2727% <100.0000%> (+1.0823%) ⬆️

... and 8 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Aug 18, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 99aff08
▶️ Tests: 110480 executed
⚪️ Checks: 31/31 completed


Learn more about TestLens at testlens.app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants