spring-projects / spring-projects/spring-framework
WebSocketMessagingAutoConfiguration causes Executor bean to be registered multiple times under different names
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 60.2k
- Forks
- 38.8k
- Avg merge
- 5d 2h
- Merged PRs (30d)
- 27
Description
Bug Description
WebSocketMessagingAutoConfiguration is using an existing registered Executor bean as the output from bean factory methods AbstractMessageBrokerConfiguration#clientInboundChannelExecutor and AbstractMessageBrokerConfiguration#clientOutboundChannelExecutor which are annotated with @Bean.
This causes the existing registered executor bean to go through the standard bean lifecycle interfaces multiple times. In addition the AbstractMessageBrokerConfiguration is also modifying the bean depending on the executor implementation.
In the case of ThreadPoolTaskExecutor it has several lifecycle interfaces such as BeanNameAware, InitializingBean, ApplicationContextAware, DisposableBean and ApplicationListener which get called for each registration.
In short registering a single instances of a bean multiple times can lead to unexpected issues and would assume most beans are not designed with this intent. AbstractMessageBrokerConfiguration is expecting separate unregistered Executor instances that it will register as beans.
I tripped over this while trying to debug an issue preventing the our application from shutting down. I first noticed the bean name on the applicationTaskExecutor - ThreadPoolTaskExecutor had a bean name of clientOutboundChannelExecutor set inside the instance. Then I figured out the reason why our app was not shutting down was the destroy method of the executor was being called multiple times due to being registered multiple times in the context.
Replication
https://github.com/emopti-jrufer/spring-boot/tree/executer_bug
See org.springframework.boot.websocket.autoconfigure.servlet.WebSocketMessagingAutoConfigurationTests#bug
Workaround
As a workaround I excluded WebSocketMessagingAutoConfiguration and had to configure the message converters in a separate WebSocketMessageBrokerConfigurer.
Contributor guide
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 WebSocketMessagingAutoConfiguration and the clientInboundChannelExecutor and clientOutboundChannelExecutor bean methods in AbstractMessageBrokerConfiguration. Run org.springframework.boot.websocket.autoconfigure.servlet.WebSocketMessagingAutoConfigurationTests#bug from the linked replication and trace the executor bean lifecycle. Done means one registered executor is not processed or destroyed multiple times and the shutdown issue is no longer reproduced.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100