[system-health] Fix file handle leak (#10059)

- Why I did it
swsscommon.ConfigDBConnector does not automatically close connection when the instance is recycled by python. So, it should not create this instance each time calling check_services. It will cause error like Failed to read from file /var/run/hw-management/led/led_status_capability - OSError(24, 'Too many open files')

- How I did it
Only connect DB once in init

- How to verify it
Manual test
This commit is contained in:
Junchao-Mellanox 2022-02-24 17:29:59 +08:00 committed by Arvindsrinivasan Lakshmi Narasimhan
parent 212cdfbe80
commit 74c49a7682

View File

@ -51,6 +51,8 @@ class ServiceChecker(HealthChecker):
self.need_save_cache = False
self.config_db = None
self.load_critical_process_cache()
def get_expected_running_containers(self, feature_table):
@ -248,9 +250,10 @@ class ServiceChecker(HealthChecker):
Args:
config (config.Config): Health checker configuration.
"""
config_db = swsscommon.ConfigDBConnector()
config_db.connect()
feature_table = config_db.get_table("FEATURE")
if not self.config_db:
self.config_db = swsscommon.ConfigDBConnector()
self.config_db.connect()
feature_table = self.config_db.get_table("FEATURE")
expected_running_containers, self.container_feature_dict = self.get_expected_running_containers(feature_table)
current_running_containers = self.get_current_running_containers()