Skip to content

[java] Fix #7117: ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to - #7118

Merged
UncleOwen merged 3 commits into
pmd:mainfrom
Eljees:fix/4815-lambda-invoked-in-calling-thread
Sep 30, 2026
Merged

UncleOwen merged 3 commits into
pmd:mainfrom
Eljees:fix/4815-lambda-invoked-in-calling-thread

Conversation

@Eljees

@Eljees Eljees commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

#4815 was a false positive: a throw inside a lambda passed to an executor was matched against the try around the call. #7105 stopped the walk at every lambda body, which fixed that, and left a false negative behind - pointed out by @UncleOwen in the review of #7105, with spring-framework's LeakAwareNettyDataBufferFactory#checkForLeaks (v5.3.13, line 56) as the example:

try {
    this.created.forEach(info -> {
        if (((PooledDataBuffer) info.getDataBuffer()).isAllocated()) {
            throw info.getError();
        }
    });
    break;
}
catch (AssertionError ex) { ... }

Iterable.forEach calls the lambda it is passed and relays an exception thrown by it to the caller, so that throw is caught by the catch right below it - the control flow this rule is about. Per @UncleOwen, the criterion is the contract of the method the lambda is passed to. @zbynek added that all methods of Stream that take a lambda should be treated the same.

The walk now continues through a lambda that is an argument of a call whose method calls that argument: Iterable.forEach, Iterator.forEachRemaining, Map.forEach, Optional.ifPresent/ifPresentOrElse, and every Stream method that takes a lambda argument. A terminal operation calls the action while the call is evaluated; an intermediate operation is evaluated by the terminal operation of the same pipeline. Every other lambda stays opaque, so #4815 does not come back. +75/-3 in ExceptionAsFlowControlRule.

Tests

Four cases added to ExceptionAsFlowControl.xml:

  • the spring-framework code above: one violation on the catch. With the rule as on main the same fixture fails with Expected 1 problem(s), 0 problem(s) found;
  • the same forEach, but the exception type is not caught there: no violation, so the rule is not switched off inside forEach;
  • a Stream.map pipeline inside the try: one violation on the catch;
  • the same map, but the exception type is not caught there: no violation.

The two cases from #4815 are unchanged.

ExceptionAsFlowControlTest: 13 tests, 0 failures. With the rule as on current main (7774e57) the same fixtures fail in two cases, with Expected 1 problem(s), 0 problem(s) found.

The rule was also run through the PMD API on the whole spring-framework file (127 lines): no violation with the rule as on main, one violation at line 61 with this patch.

The branch is rebased on current main; docs/pages/release_notes.md lists the issue under Fixed Issues and the new reporting behaviour under Changed Rules.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@pmd-actions-helper

pmd-actions-helper Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 1 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-09-30 17:50:47+00:00 for dfab14b)

@UncleOwen

Copy link
Copy Markdown
Member

Is this really about "in the same thread"? I'd say it's more about the fact that the lambda gets called by the method its passed to (e.g. forEach).

@zbynek

zbynek commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Probably all methods of Stream that accept a lambda should be treated the same.

…hod it is passed to

pmd#7105 stopped the walk at every lambda body. That fixed the false positive of
pmd#4815, but it also stopped reporting a throw inside a lambda that the method it
is passed to calls itself. spring-framework's
LeakAwareNettyDataBufferFactory#checkForLeaks (v5.3.13, line 56) throws inside
Iterable.forEach and catches the exception right below.

The walk now continues through a lambda that is an argument of a call whose
method calls that argument: Iterable.forEach, Iterator.forEachRemaining,
Map.forEach, Optional.ifPresent/ifPresentOrElse, and the Stream methods that
take a lambda argument. A terminal operation calls the action while the call is
evaluated; an intermediate operation is evaluated by the terminal operation of
the same pipeline. Every other lambda stays opaque, so pmd#4815 does not come back.

Signed-off-by: Eljees <[email protected]>
@Eljees
Eljees force-pushed the fix/4815-lambda-invoked-in-calling-thread branch from 0b03905 to 4e64d1a Compare September 28, 2026 08:06
@Eljees Eljees changed the title [java] ExceptionAsFlowControl: a throw in a lambda that runs in the calling thread [java] ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to Sep 28, 2026
@Eljees

Eljees commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Both review points are addressed and pushed:

  • the criterion is now the contract of the method the lambda is passed to (@UncleOwen): the title, the description, the rule's comment and the test case names say "invoked by the method it is passed to" instead of "in the same thread";
  • all methods of Stream that take a lambda argument are treated the same (@zbynek): forEach/forEachOrdered, filter, map, peek, sorted, takeWhile, reduce, collect, anyMatch and the rest. A terminal operation calls the action while the call is evaluated; an intermediate operation is evaluated by the terminal operation of the same pipeline.

ExceptionAsFlowControlTest is 13 cases and green; with the rule as on main two of them fail (the forEach one and the map one). The branch is rebased on current main, the PR title and description are reworded, and the release notes have entries under Fixed Issues and Changed Rules.

Comment thread docs/pages/release_notes.md Outdated
Comment on lines +43 to +45
* The Java rule {% rule java/design/ExceptionAsFlowControl %} now reports exceptions thrown inside a lambda
that the method it is passed to calls itself, such as `Iterable.forEach` or a `Stream` operation that
takes a lambda. Exceptions thrown inside any other lambda are still not reported.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did so in the current, release version as well. So this isn't really a change.

Comment on lines +42 to +76
private static final Set<String> CALLS_THE_LAMBDA_ARGUMENT = new HashSet<>(Arrays.asList(
"java.lang.Iterable#forEach",
"java.util.Iterator#forEachRemaining",
"java.util.Map#forEach",
"java.util.Optional#ifPresent",
"java.util.Optional#ifPresentOrElse",
"java.util.stream.Stream#allMatch",
"java.util.stream.Stream#anyMatch",
"java.util.stream.Stream#collect",
"java.util.stream.Stream#dropWhile",
"java.util.stream.Stream#filter",
"java.util.stream.Stream#flatMap",
"java.util.stream.Stream#flatMapToDouble",
"java.util.stream.Stream#flatMapToInt",
"java.util.stream.Stream#flatMapToLong",
"java.util.stream.Stream#forEach",
"java.util.stream.Stream#forEachOrdered",
"java.util.stream.Stream#generate",
"java.util.stream.Stream#iterate",
"java.util.stream.Stream#map",
"java.util.stream.Stream#mapMulti",
"java.util.stream.Stream#mapMultiToDouble",
"java.util.stream.Stream#mapMultiToInt",
"java.util.stream.Stream#mapMultiToLong",
"java.util.stream.Stream#mapToDouble",
"java.util.stream.Stream#mapToInt",
"java.util.stream.Stream#mapToLong",
"java.util.stream.Stream#max",
"java.util.stream.Stream#min",
"java.util.stream.Stream#noneMatch",
"java.util.stream.Stream#peek",
"java.util.stream.Stream#reduce",
"java.util.stream.Stream#sorted",
"java.util.stream.Stream#takeWhile"
));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really looks like a CompoundInvocationMatcher. Would also simplify lines 131-135.

@Eljees

Eljees commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Replaced the manual declaring-type/method-name lookup with CompoundInvocationMatcher and its anyMatch call. Removed the Changed Rules entry and retained the Fixed Issues entry. Rebuilt the Java module and ran ExceptionAsFlowControlTest: all 13 tests pass.

@UncleOwen UncleOwen added this to the 7.29.0 milestone Sep 30, 2026

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen
UncleOwen merged commit 5c9271c into pmd:main Sep 30, 2026
13 checks passed
@adangel adangel changed the title [java] ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to [java] Fix #7117: ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to Oct 1, 2026
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.

[java] ExceptionAsFlowControl: False negative when the lambda is invoked by the method it is passed to

3 participants