Skip to content

gRPC client 'grpc' load balancing fails to bind with a NullPointerException when no child policies are configured - #52193

Merged
philwebb merged 2 commits into
spring-projects:4.1.xfrom
CCCLL:fix/grpc-load-balancing-child
Oct 17, 2026
Merged

philwebb merged 2 commits into
spring-projects:4.1.xfrom
CCCLL:fix/grpc-load-balancing-child

Conversation

@CCCLL

@CCCLL CCCLL commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Configuring grpc load balancing for a gRPC client channel without any child policies fails. For example:

spring:
  grpc:
    client:
      channel:
        default:
          service-config:
            loadbalancing:
            - grpc:
                service-name: lb

results in a BindException that is caused by:

java.lang.NullPointerException: Cannot invoke "java.util.List.forEach(java.util.function.Consumer)" because "child" is null
	at org.springframework.boot.grpc.client.autoconfigure.ServiceConfig$LoadBalancingConfig$GrpcLoadBalancingConfig.<init>(ServiceConfig.java:232)

The compact constructor of GrpcLoadBalancingConfig validates the children without checking that any were bound. The rest of the record already copes with child being null, and childPolicy is optional for grpc-java's GrpclbLoadBalancerProvider, which uses round robin when it is absent.

This change only validates the children when they are present.

Validation:

  • The new grpcLoadBalancing test (grpc: {}) fails before the fix with the exception above and passes afterwards.
  • :module:spring-boot-grpc-client:check passed (120 tests).
  • Binding spring.grpc.client.channel.default.service-config.loadbalancing[0].grpc.service-name=lb to GrpcClientProperties fails in the same way before the fix. Afterwards, the resulting service config is {loadBalancingConfig=[{grpclb={serviceName=lb}}]}.

Tested on 4.1.x (27279e255fd) with JDK 25.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. label Oct 13, 2026
@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. labels Oct 14, 2026
@snicoll snicoll modified the milestones: 4.0.x, 4.1.x Oct 14, 2026
CCCLL and others added 2 commits October 16, 2026 19:02
Update `ServiceConfig` to protect against an NPE when no children
are present.

Child policies are optional and grpc-java uses round robin when none
are configured, so only validate them when they are present.

See spring-projectsgh-52193

Signed-off-by: Jialin Chen <cccll2019@gmail.com>
@philwebb
philwebb force-pushed the fix/grpc-load-balancing-child branch from d4378a2 to 8d74332 Compare October 17, 2026 02:03
@philwebb
philwebb merged commit 80855b6 into spring-projects:4.1.x Oct 17, 2026
0 of 2 checks passed
@snicoll snicoll modified the milestones: 4.1.x, 4.1.2 Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants