I have encountered an issue in my Spring Batch job where, intermittently, the error "Singleton bean creation not allowed while the singletons of this factory are in destruction" occurs with the StatsdMeterRegistry bean.

This issue seems to arise when the application unexpectedly receives a SIGTERM signal (such as when a node goes down in a cloud environment) and begins the shutdown process, some background tasks in the StatsdMeterRegistry are not properly terminated before the bean destruction phase. As a result, there appears to be an attempt to access the StatsdMeterRegistry after the ApplicationContext has already started to shut down.

To address this, I believe it would be beneficial to gracefully shut down the StatsdMeterRegistry before the bean is destroyed. A possible solution could be to modify the StatsdMetricsExportAutoConfiguration to ensure that StatsdMeterRegistry.close() is called during the shutdown phase, as shown below:

    @PreDestroy
    public void destroy(StatsdMeterRegistry statsdMeterRegistry) {
        statsdMeterRegistry.close();
    }

This would help to properly terminate any ongoing background tasks and prevent the issue from occurring during application shutdown.

Thank you for considering this PR. I look forward to your feedback!

Comment From: pivotal-cla

@popotec Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: pivotal-cla

@popotec Thank you for signing the Contributor License Agreement!

Comment From: wilkinsona

Thanks for the PR, @popotec, but as discussed I don't think this is the right solution to your problem. Please do open an issue if you'd like us to investigate further.