Pre-create the shutdown Thread #86
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
On some condition, we can get the following Error in the application logs
Investigations shows that the Thread being created is a Thread that will have to shutdown stuff in RabbitMQ Client.
And that the condition for this to occur, is the tomcat container being shutdown by Cloud Foundry.
It appears that the ThreadFactory cannot create new Native Threads while the container is being shutdown. At the same time, the application tries to shutdown allocated resources and running processes. So when RabbitMQ client tries to shutdown, it creates a new Thread that will do those shutdown tasks.
But as a new Native Thread cannot be created, the Threads fails to start, and the shutdown task is not executed.
As the condtion for this to occur is that the application and JVM is shutting down, this may sound like it's not a real issue. In fact, I am concerned by the
OutOfMemoryError
appearing in the logs. Logs are monitored and trigger alerts, so you don't want to see this in your logs unless it's a real problem.As a proof of concept, I patched the RabbitMQ client to create the shutdown Thread as soon at the ConnectionFactory is created : ie a ThreadPool of size 1 is created with the ConnectionFactory, so that the native Thread is created and is ready to execute any task submited to it.
It turned out that this patch did fix the problem: there is no OOM Error at application shutdown anymore.
I don't know for sure if the fix is good enough for every cases.
Like, this patch makes a new Thread waiting for the whole life of the ConnectionFactory. As if you have multiple ConnectionFactories in your app, you have the same number of ThreadPools waiting for the shutdown task.
I guess a typical application would only have only 2 ConnectionFactories anyways (1 for consumers and 1 for producers) so this may not be a big overhead. And maybe we can rework the code to ensure we only use as much as 1 ThreadPool for every ConnectionFactories in an application ?
Hoping it's a good start anyways.
Please review and comment.