spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

WeightCalculatorWebFilter.onApplicationEvent method causes a deadlock

Open cxzucc opened this issue 4 years ago • 3 comments

	@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());
			}
		}
	}

  1. When application starting up, the main thread hold a lock in DefaultSingletonBeanRegistry.getSingleton method.
  2. 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).
  3. 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 avatar Mar 29 '22 01:03 cxzucc

@cxzucc please, mention here how the issue was fixed

ivan-osipov avatar Dec 16 '22 08:12 ivan-osipov

add this to yml or properties file

spring.cloud.gateway.predicate.weight.enabled=false

cxzucc avatar Dec 19 '22 02:12 cxzucc

@cxzucc please, mention here how the issue was fixed

spring.cloud.gateway.predicate.weight.enabled=false

cxzucc avatar Dec 19 '22 02:12 cxzucc