Skip gRPC port initializer when spring-grpc is absent - #51174
Open
r0h1tb wants to merge 1 commit into
Open
Conversation
GrpcPortInfoApplicationContextInitializer is registered unconditionally via
META-INF/spring.factories, and its initialize() unconditionally adds a listener
declared as ApplicationListener<GrpcServerStartedEvent>.
When spring-boot-grpc-test is on the classpath but spring-grpc is not, Spring
resolves that listener's generic interface to decide whether to deliver an
event, and Class.getGenericInterfaces() throws:
java.lang.TypeNotPresentException: Type
org.springframework.grpc.server.lifecycle.GrpcServerStartedEvent not present
at java.base/java.lang.Class.getGenericInterfaces(Class.java:1278)
at org.springframework.core.ResolvableType.getInterfaces(ResolvableType.java:544)
Guard registration on the event type being present, using ClassUtils.isPresent
as LogbackLoggingSystem already does for the SLF4J bridge handler.
See spring-projectsgh-50825
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See gh-50825
Problem
GrpcPortInfoApplicationContextInitializeris registered unconditionally viaMETA-INF/spring.factories, and itsinitialize()unconditionally adds a listener declared asApplicationListener<GrpcServerStartedEvent>.With
spring-boot-grpc-teston the classpath butspring-grpcabsent, Spring resolves that listener's generic interface to decide whether to deliver an event, andClass.getGenericInterfaces()throws — which is the stack trace in the report:Fix
Guard registration on the event type being present:
ClassUtils.isPresentrather than a try/catch because that's whatLogbackLoggingSystem.isBridgeHandlerAvailable()already does for the SLF4J bridge handler — same situation, optional class reached through a factories-registered component.The classloader comes from the context rather than the class, so the guard sees the same classpath the listener resolution will.
Scope
Deliberately narrow: only the initializer is guarded. I did not touch the
spring.factoriesregistration itself, since the initializer being constructed is harmless — it's only the listener whose generic type can't resolve. Happy to take a different approach if you'd rather condition the registration.Tests
Two added to
GrpcPortInfoApplicationContextInitializerTests, using the existingFilteredClassLoader:whenGrpcServerStartedEventIsNotOnTheClasspathRegistersNoListener— hidesGrpcServerStartedEvent, asserts no listener is registeredwhenGrpcServerStartedEventIsOnTheClasspathRegistersListener— control, so the guard can't silently disable the featureWith the source change stashed but both tests kept, the first fails:
Verification
./gradlew :module:spring-boot-grpc-test:testmain, unmodified)The third row is what shows the tests pin the reported bug rather than restating the fix.
I ran the affected module's suite rather than the full build. Happy to run anything wider you'd like to see.