Skip to content

Debugger shows stale values for variables when a lambda is entered twice #795

@sythe

Description

@sythe

If I define a lambda that references some variable outside of the lambda, then the debugger will show the stale values for that variable upon entering the lambda a second time. Example:

package playground;

import java.util.function.Function;

public class LambdaTest {
    public static void main(String[] args) {
        try {
            Function<String, String> messengerA = createMessenger("A");
            Function<String, String> messengerB = createMessenger("B");
            
            System.out.println(messengerA.apply("Hi Matt"));
            System.out.println(messengerB.apply("Hi Raja"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    static public Function<String, String> createMessenger(String messengerName) {
        return message -> {
            System.out.println("Inside Transformer " + messengerName);
            return message + " - From Transformer " + messengerName;
        };
    }
}

In the code above, setting a breakpoint on line 21 (return message + " - From Transformer " + messengerName;) and executing the code will cause the program to stop twice on line 21. The first time (when called from line 11), using the debugger to examine the messengerName variable shows the correct value of "A". Allowing the code to continue will cause the debugger to stop again on line 21, this time being called from line 12. This time, the value messengerName should be "B", but the debugger reports it as "A". (The variable will also show as "A" in the expressions view.) This is true even though the previous statement prints "Inside Transformer B", so the code has the correct value. It is just showing wrong in the debugger views.

I am running Eclipse 2025-09 and Java 1.8 (see screenshots for more environment details).

Image Image Image Image

LambdaTest.java

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions