From b3a283366c21a49ce05a79ad3fb0633659ae5dea Mon Sep 17 00:00:00 2001 From: Stephen Sun <5379172+stephenxs@users.noreply.github.com> Date: Thu, 29 Apr 2021 16:33:39 +0800 Subject: [PATCH] Fix issue: exception occurred during chassis object being destroyed (#7446) The following error message is observed during chassis object being destroyed "Exception ignored in: Traceback (most recent call last): File "/usr/local/lib/python3.7/dist-packages/sonic_platform/chassis.py", line 83, in __del__ ImportError: sys.meta_path is None, Python is likely shutting down The chassis tries to import deinitialize_sdk_handle during being destroyed for the purpose of releasing the sdk_handle. However, importing another module during shutting down can cause the error because some of the fundamental infrastructures are no longer available." This error occurs when a chassis object is created and then destroyed in the Python shell. - How I did it To fix it, record the deinitialize_sdk_handle in the chassis object when sdk_handle is being initialized and call the deinitialize handler when the chassis object is being destroyed - How to verify it Manually test. --- .../mlnx-platform-api/sonic_platform/chassis.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py index 585a664efc..2ae93e14d4 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py @@ -73,6 +73,7 @@ class Chassis(ChassisBase): self.sfp_event_initialized = False self.reboot_cause_initialized = False self.sdk_handle = None + self.deinitialize_sdk_handle = None logger.log_info("Chassis loaded successfully") @@ -80,9 +81,8 @@ class Chassis(ChassisBase): if self.sfp_event_initialized: self.sfp_event.deinitialize() - if self.sdk_handle: - from sonic_platform.sfp import deinitialize_sdk_handle - deinitialize_sdk_handle(self.sdk_handle) + if self.deinitialize_sdk_handle: + self.deinitialize_sdk_handle(self.sdk_handle) def initialize_psu(self): @@ -140,10 +140,12 @@ class Chassis(ChassisBase): def get_sdk_handle(self): if not self.sdk_handle: - from sonic_platform.sfp import initialize_sdk_handle + from sonic_platform.sfp import initialize_sdk_handle, deinitialize_sdk_handle self.sdk_handle = initialize_sdk_handle() if self.sdk_handle is None: logger.log_error('Failed to open SDK handle') + else: + self.deinitialize_sdk_handle = deinitialize_sdk_handle return self.sdk_handle