spring-cloud-gateway
spring-cloud-gateway copied to clipboard
WeightCalculatorWebFilter.onApplicationEvent method causes a deadlock
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof PredicateArgsEvent) {
handle((PredicateArgsEvent) event);
}
else if (event instanceof WeightDefinedEvent) {
addWeightConfig(((WeightDefinedEvent) event).getWeightConfig());
}
else if (event instanceof RefreshRoutesEvent && routeLocator != null) {
// forces initialization
if (routeLocatorInitialized.compareAndSet(false, true)) {
// on first time, block so that app fails to start if there are errors in
// routes
// see gh-1574
routeLocator.ifAvailable(locator -> locator.getRoutes().blockLast());
}
else {
// this preserves previous behaviour on refresh, this could likely go away
routeLocator.ifAvailable(locator -> locator.getRoutes().subscribe());
}
}
}
- When application starting up, the main thread hold a lock in DefaultSingletonBeanRegistry.getSingleton method.
- If there's a RefreshRoutesEvent received by WeightCalculatorWebFilter(In my project, openfeign publish this event), it will call blockLast(), whick actually called CountDownLatch.wait(), so main thread will hang up and unreleased the lock (LockSupport.Park).
- Another thread try to obtain the lock which was locked by main thread DefaultSingletonBeanRegistry.getSingleton. A call chain like: RouteDefinitionRouteLocator.convertToRoute() -> combinePredicates() -> lookup() -> ConfigurationService.bind() -> doBind() -> ObjectProvider.getIfAvailable
@cxzucc please, mention here how the issue was fixed
add this to yml or properties file
spring.cloud.gateway.predicate.weight.enabled=false
@cxzucc please, mention here how the issue was fixed
spring.cloud.gateway.predicate.weight.enabled=false