Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bringyou opened this issue Feb 7, 2025 · 2 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@bringyou
Copy link

bringyou commented Feb 7, 2025

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 7, 2025
@sdeleuze
Copy link
Contributor

Could you please share a reproducer (attached archive or link to a repository) that shows a concrete use-case where that causes a problem?

@sdeleuze sdeleuze added status: waiting-for-feedback We need additional information before we can continue in: core Issues in core modules (aop, beans, core, context, expression) labels Feb 11, 2025
@bringyou
Copy link
Author

bringyou commented Feb 13, 2025

@sdeleuze sure, I've upload a demo project
please pay attention on this file
on it's destroy method we will prove that it's dependent lifecycle bean is closed earlier.

Image Image

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

3 participants