Skip to content

[java] Fix #4815: ExceptionAsFlowControl: stop at a lambda body - #7105

Merged
UncleOwen merged 5 commits into
pmd:mainfrom
Eljees:fix/4815-exception-flow-control-lambda
Sep 26, 2026
Merged

UncleOwen merged 5 commits into
pmd:mainfrom
Eljees:fix/4815-exception-flow-control-lambda

Conversation

@Eljees

@Eljees Eljees commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Describe the PR

ExceptionAsFlowControl walks up from a throw to the enclosing body declaration and reports any try on the way whose catch would catch the thrown type. The walk did not stop at a lambda expression, so in the code from #4815

try {
    es.execute(() -> {
        try {
            unsafeDoPublish(details);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
} catch (RuntimeException e) {
    LOGGER.warn("Issue publishing notification " + details, e);
}

the throw inside the lambda was matched against the outer catch (RuntimeException e). That catch is not where the exception goes: the lambda body runs when the executor invokes it, on another thread here, and the exception propagates to that caller.

The rule already stops at a body declaration, which covers anonymous and local classes. This makes it stop at a lambda too: whoever invokes the lambda is not visible from the throw site, so the rule cannot tell whether a surrounding try catches the exception, and it now leaves that case alone. +7/-0 in ExceptionAsFlowControlRule.

Tests

Two cases in ExceptionAsFlowControl.xml:

ExceptionAsFlowControlTest: 9 tests, 0 failures.

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)

An exception thrown in a lambda body propagates to whoever invokes the
lambda, not to the try statement around the lambda expression.

Fixes pmd#4815
@pmd-actions-helper

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

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 8 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-26 09:33:55+00:00 for 817461b)

@UncleOwen UncleOwen added this to the 7.29.0 milestone Sep 26, 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!

This could be considered a new false-negative.

I'll still merge this, because it's definitely an improvement.

@Eljees

Eljees commented Sep 27, 2026

Copy link
Copy Markdown
Contributor Author

I looked into the false negative you pointed at. It reproduces on the merged rule: on LeakAwareNettyDataBufferFactory#checkForLeaks the rule reports nothing, because the walk stops at the lambda body. Iterable.forEach performs the action in the calling thread and relays an exception thrown by it to the caller, so that throw is caught by the catch right below it.

Filed #7117 with a fix that keeps the walk going through lambdas passed to the JDK methods whose contract says the action runs in the calling thread; every other lambda stays opaque. #7118 is the PR.

Eljees added a commit to Eljees/pmd that referenced this pull request Sep 28, 2026
…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]>
@adangel adangel changed the title [java] ExceptionAsFlowControl: stop at a lambda body [java] Fix #4815: ExceptionAsFlowControl: stop at a lambda body Sep 28, 2026
UncleOwen pushed a commit that referenced this pull request Sep 30, 2026
…hod it is passed to (#7118)

* [java] ExceptionAsFlowControl: a throw in a lambda invoked by the method it is passed to

#7105 stopped the walk at every lambda body. That fixed the false positive of
#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 #4815 does not come back.

Signed-off-by: Eljees <[email protected]>

* Use compound invocation matcher and clarify release notes

* [java] Handle primitive streams in ExceptionAsFlowControl

---------

Signed-off-by: Eljees <[email protected]>
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 positive on Lambda/asynchronous

2 participants