spring-cloud / spring-cloud/spring-cloud-stream
Change in bean lifecycle due to DefaultBinderFactory causes unexpected startup of LifeCycle beans
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 646
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 8
Description
After the change introduced in https://github.com/spring-cloud/spring-cloud-stream/commit/f259c557045c91abf813214f91a089d0a885a825, we have detected a change in behavior during the startup of Spring Boot applications.
Prior to this change, when refreshContext was executed in SpringApplication, it eventually called the onRefresh method in DefaultLifecycleProcessor. This method only forces the startup of SmartLifeCycle beans where isAutoStartup() returns true, ignoring the rest.
LifeCycle beans that are not SmartLifeCycle were not started at this point.
As of the mentioned commit, DefaultBinderFactory becomes a SmartLifeCycle bean, triggering the startup of other ConfigurationApplicationContext instances, and in this second call, the start() method is invoked instead of onRefresh(). This results in startBeans(false) being called in DefaultLifecycleProcessor, which also forces the startup of LifeCycle beans that are not SmartLifeCycle.
A concrete example: the integrationHeaderChannelRegistry bean (spring-integration-core), which previously was instantiated but never started and did not schedule its 'reaper' task. Now it does start, launches its 'reaper' task, and causes issues such as the one reported in https://github.com/spring-projects/spring-integration/issues/10547.
Describe the problem
This change in behavior may cause unexpected side effects in LifeCycle beans that are not SmartLifeCycle, such as undesired scheduled tasks.
References
- Commit with the change: https://github.com/spring-cloud/spring-cloud-stream/commit/f259c557045c91abf813214f91a089d0a885a825
- Related implementations:
- refreshContext in SpringApplication (https://github.com/spring-projects/spring-boot/blob/ff4889217c7f1229564a6e94560c05f43ab10b13/core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java#L321)
- onRefresh and start in DefaultLifecycleProcessor (https://github.com/spring-projects/spring-framework/blob/9a54fac9982c427746f150d5a9cf11b5a6eeff8e/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java#L295)
- integrationHeaderChannelRegistry in spring-integration-core
- Related issue: https://github.com/spring-projects/spring-integration/issues/10547
Steps to reproduce
- Start a Spring Boot application with spring-cloud-stream version (4.3.X).
- Verify that integrationHeaderChannelRegistry starts and launches its "reaper" task.
- This can be verified by enabling TRACE log for
org.springframework.integration.channel.DefaultHeaderChannelRegistry.
- This can be verified by enabling TRACE log for
- Start the same Spring Boot application with spring-cloud-stream version (4.2.X).
- Verify that integrationHeaderChannelRegistry does not start and does not launch its "reaper" task.
- No traces will be shown for the log
org.springframework.integration.channel.DefaultHeaderChannelRegistry: TRACE
- No traces will be shown for the log
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the DefaultBinderFactory change in commit f259c557045c91abf813214f91a089d0a885a825, then trace refreshContext, DefaultLifecycleProcessor.onRefresh and start, and the integrationHeaderChannelRegistry behavior described in the issue. Reproduce with Spring Cloud Stream 4.3.X and 4.2.X using TRACE logging for org.springframework.integration.channel.DefaultHeaderChannelRegistry. Done means the unexpected lifecycle startup and reaper task behavior is resolved without the reported side effects.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- backend, distributed-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100