Skip to content

Lifecycle beans won't stop dependent beans which are not SmartLifecycle type when context closing #34382

Closed as not planned
@bringyou

Description

@bringyou

When closing the applicationContext, those Lifecycle beans are stopped earlier than normal beans.

// AbstractApplicationContext.doClose
protected void doClose() {
    publishEvent(new ContextClosedEvent(this));
	if (this.lifecycleProcessor != null) {
		try {
			this.lifecycleProcessor.onClose();
		}catch (Throwable ex) {}
	}

	destroyBeans();
	closeBeanFactory();
	onClose();
	resetCommonCaches();
}

And the default lifecycle processor only close those beans which are Lifecycle

// DefaultLifecycleProcessor
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName,
			final CountDownLatch latch, final Set<String> countDownBeanNames) {

		Lifecycle bean = lifecycleBeans.remove(beanName);
		if (bean != null) {
			String[] dependentBeans = getBeanFactory().getDependentBeans(beanName);
			for (String dependentBean : dependentBeans) {
				// so if the dependentBean is not LifeCycle type, it will be skipped to stop
				doStop(lifecycleBeans, dependentBean, latch, countDownBeanNames);
			}
			// codes which are doing the cleanup
			// ...
		}
	}

If normal beans which depend on Lifecycle beans may not work properly during the closing period, because their depending Lifecycle beans are stopped earlier.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: invalidAn issue that we don't feel is valid

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions