Repository navigation
GROOVY-12687: STC: no StackOverflowError when a type checking extension reaches the error tolerance - #3206
Merged
paulk-asert merged 1 commit intoNov 4, 2026
Conversation
β¦on reaches the error tolerance A type checking extension's event handlers run through AbstractTypeCheckingExtension.safeCall, which records any exception they throw as an error of the source unit. When an error the extension reports reaches the error tolerance, the error collector throws a MultipleCompilationErrorsException carrying itself, and safeCall recorded that exception in the same collector. Rendering the errors then rendered the exception, whose message renders the collector again, so compilation ended in a StackOverflowError with no diagnostics. A fatal error reported from an extension failed the same way. safeCall now rethrows such an exception, as its errors are already in the collector. A failure from a separate compilation is still recorded.
Codecov Reportβ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3206 +/- ##
==================================================
- Coverage 74.1518% 74.1497% -0.0022%
+ Complexity 43728 43726 -2
==================================================
Files 1692 1692
Lines 149527 149530 +3
Branches 28360 28362 +2
==================================================
- Hits 110877 110876 -1
- Misses 28438 28441 +3
- Partials 10212 10213 +1
π New features to boost your workflow:
|
β All tests passed βπ·οΈ Commit: a302ea3 Learn more about TestLens at testlens.app/docs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JIRA: https://issues.apache.org/jira/browse/GROOVY-12687
The bug
When an error reported by a type checking extension reaches the compiler's error tolerance
(10 by default), compilation ends in a
StackOverflowErrorand none of the collected errors isreported. Ten errors from
groovy.typecheckers.NullCheckerin one method are enough. Anytolerance above zero is affected, not only the default (
--tolerance=3fails the same way), andso is a fatal error reported from an extension (
sourceUnit.addFatalError(...)), whatever thetolerance.
Reproducer
TenOnly.groovy:With 6.0.0 and with master (9a1a0d0) it exits 1 after printing 1,030 lines of stack trace
and no diagnostic:
Expected, and printed with this change: the ten
Potential null dereference: 'a' is @Nullablediagnostics and
10 errors, as for ordinary type checking errors.Neither
--checknor the annotation is needed. The same class without@TypeChecked, compiledwith
--configscriptapplyingast(groovy.transform.TypeChecked, extensions: ['groovy.typecheckers.NullChecker']), fails thesame way with the default error format (that cycle goes through
ErrorFormat.writeFullinstead).Nine errors,
--tolerance=0, and twelve ordinary@TypeCheckederrors with no extension werealready reported correctly, and their output is unchanged.
Cause
An extension's event handlers (
afterVisitMethod,onMethodSelectionand the rest) are run byAbstractTypeCheckingExtension.safeCall, which catches anyExceptionand records it withSourceUnit.addException. When an error the extension reports reaches the tolerance,ErrorCollector.addErrorthrows aMultipleCompilationErrorsExceptionthat carries thecollector itself, and
safeCallrecords that exception, as anExceptionMessage, in the samecollector. Rendering the collector renders the exception's message, which renders the collector
again.
The path is old: a fatal error reported from an extension overflows the same way on 5.0.8 and
6.0.0-beta-1. GROOVY-12306 (6.0.0-RC-1), which made type checking errors count toward the
tolerance, is what lets ordinary extension errors reach it.
Fix
safeCallnow rethrows aMultipleCompilationErrorsExceptionthat carries the source unit'sown error collector, because its errors are already recorded there. It then propagates the way
it does when the type checker itself reaches the tolerance. Any other exception is recorded as
before.
The check compares collectors rather than only exception types.
CompilationUnit's phase loopdiscards a
CompilationFailedExceptionon the assumption that its errors are already collected,so rethrowing every
CompilationFailedExceptionwould lose a failure from a separate compilationthat an extension runs. This was checked with an extension that calls
new GroovyShell().parse('class {'). If every such exception is rethrown, the class compileswith exit 0 and is written out. With this change it still reports
General error during canonicalization: startup failed: ... Missing '}', exactly as before.Test
ErrorToleranceTest.testExtensionErrorsHonourTolerancecompiles a class with 14 methods, using aprecompiled extension that reports one error per method from
afterVisitMethod, at the defaulttolerance. It checks four things:
DEFAULT_TOLERANCEerrors are collected;SyntaxErrorMessages;Without the fix, the test fails with
StackOverflowErrorinMultipleCompilationErrorsException.getMessage.Commands and results (JDK 21.0.12, macOS,
--max-workers=2):./gradlew :test --tests org.codehaus.groovy.control.ErrorToleranceTest: 7 tests. Without thefix, 1 failed (the new test, with
java.lang.StackOverflowError). With the fix, all 7 pass../gradlew :test :groovy-typecheckers:test:BUILD SUCCESSFUL. Core ran 19,317 tests with0 failures, 0 errors and 68 skipped; groovy-typecheckers ran 386 tests with 0 failures.
./gradlew :checkstyleMain: no new violation. The class's existing fan-out warning goes from26 to 27 (limit 22) because of the one added import. The total for core main is unchanged
at 3,337.
config/codenarc/codenarc.groovy, run directly onErrorToleranceTest.groovy: one P3 (UnnecessaryDotClass) both before and after, on a linethe change only moves. The whole
:codenarcTesttask ran out of heap on this machine, so itwas not used.
./gradlew :groovy-binary:installGroovy, then the installedgroovycbefore and after thechange. On master, the installed
groovycfails at startup without JavaFX (GROOVY-12683), sothese runs used a copy of the installation without
lib/groovy-fx-*.jar.TenOnly.groovy,--check --error-format=shortStackOverflowError10 errorsTenOnly.groovy,--tolerance=3StackOverflowError3 errors--configscript, default formatStackOverflowError10 errors--configscript, short formatStackOverflowError10 errorsTenOnly.groovy,--tolerance=010 errors10 errors9 errors9 errors@TypeChecked, 12int a = 'x'10 errors10 errorssourceUnit.addFatalErrorStackOverflowError1 errorGroovyShell1 error(General error ...)Affected versions
GROOVY_6_0_X(caef1fc) has the samesafeCall,ErrorCollectorandStaticTypeCheckingVisitor.addError, so it is affected. Checked by source only, not built.10 errors), but the fatal-error caseis.
type checking errors.
Backport
Suggested target:
GROOVY_6_0_X. Both touched files are identical there and on master, andthe commit applies cleanly to caef1fc (
git apply --check). The5.x branches (
GROOVY_5_0_X,GROOVY_5_1_X) have the samesafeCall, so they could take thechange for the fatal-error case. They target Java 11, so the pattern variable would need to
become a cast there.