Session does not persist between ChainBuilders

I have created multiple ChainBuilders that utilize Pebble templates in their http requests.

Throughout the simulation I am setting variables to the session.
In the chain builders the variables I set to the session are still present but their values are set to null.

I am following the documentation when setting session variables.

Chain Builder 1

return exec(session -> {
	session = session.set("color", "green");
	session = session.set("size", 6);
	return session;
})
.other code here

Chain Builder 2

return exec(session -> {
	Session newSession = session.set("name", "PEAR");
	return newSession;
})
.other code here

Session Variables:
size: null
color:null
name:PEAR

Is this some asynchronous issue?
Does this mean I need to be extremely explicit on what I set in each session?

My idea is that if I set the size property on a session once, that all following chains can lookup size and it will be 6, until its overwritten or another session starts which could have a different size.

Please have a proper look at the documentation : Gatling - Session API

Session is immutable.

You are correct, that is why I reassign the result of x.set to the session variable. To overwrite the session after using set.

When I debug in IntelliJ. The session’s attributes map contains the keys that I set earlier, its just that their values are null. I know from earlier experience if I did not overwrite the session that the keys wouldnt exists in the sessions attribute map.

Does ChainBuilder1 and ChainBuilder2 are in the same Scenario?

  private ChainBuilder chainBuilder1() {
    return exec(session -> {
      session = session.set("color", "green");
      session = session.set("size", 6);
      return session;
    });
  }

  private ChainBuilder chainBuilder2() {
    return exec(session -> {
      Session newSession = session.set("name", "PEAR");
      return newSession;
    });
  }

  ScenarioBuilder scn = scenario("MyScenario")
    .exec(chainBuilder1())
    .exec(chainBuilder2())
  ;

because session is not shared between virtual users, it have to be defined in the same scenario.

Both ChainBuilders are executed in the same ScenarioBuilder. The chain builders are held in seperate classes.

So the ScenarioBuilder looks like

ScenarioBuilder scn = scenario("Find and Eat A Fruit")
  .exec(new FindClass().findFruit())
  .exec(new EatClass().eatFruit())

Turns out the nulls came from an incorrect Key used in a get on a Java Map, which was later used in a session set. Thats where the magic nulls came from.