Skip to content

[show][config][plugin] add processing of ModuleNotFoundError with log_warning#3832

Merged
qiluo-msft merged 2 commits intosonic-net:masterfrom
maksym-kovalchuk:plugin-not-found-error-processing
Jul 21, 2025
Merged

[show][config][plugin] add processing of ModuleNotFoundError with log_warning#3832
qiluo-msft merged 2 commits intosonic-net:masterfrom
maksym-kovalchuk:plugin-not-found-error-processing

Conversation

@maksym-kovalchuk
Copy link
Contributor

There is the case of rare situation of race condition when plugin script file is removed right before the stage of its load via cli (e.g. in case of dynamic removing with app extensions). This situation has no impact but generate additional error messages to syslog

What I did

Add separate processing ModuleNotFoundError with log_warning messages to avoid excess ERR printing

How to verify it

It happens very rarely in nature but you can just try to remove plugin script manually right before its loading stage. I used this changes on top of my commit for testing:

diff --git a/utilities_common/util_base.py b/utilities_common/util_base.py
index 95098b7..c808b0a 100644
--- a/utilities_common/util_base.py
+++ b/utilities_common/util_base.py
@@ -2,6 +2,8 @@ import os
 import pkgutil
 import importlib

+import subprocess
+
 from sonic_py_common import logger

 # Constants ====================================================================
@@ -11,6 +13,7 @@ PDDF_SUPPORT_FILE = '/usr/share/sonic/platform/pddf_support'

 log = logger.Logger()

+plugin_path = "/usr/local/lib/python3.11/dist-packages/show/plugins/dhcp-relay.py"

 class UtilHelper(object):
     def __init__(self):
@@ -28,11 +31,15 @@ class UtilHelper(object):
                 continue
             log.log_debug('importing plugin: {}'.format(module_name))
             try:
+                if module_name == "show.plugins.dhcp-relay":
+                    subprocess.run(['mv', plugin_path, '/root/' ])
                 module = importlib.import_module(module_name)

             except ModuleNotFoundError as err:
                 log.log_warning('failed to import plugin {}: {}'.format(module_name, err),
                               also_print_to_console=True)
+                if module_name == "show.plugins.dhcp-relay":
+                    subprocess.run(['mv', '/root/dhcp-relay.py', plugin_path ])
                 continue

             except Exception as err:

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Apr 7, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: maksym-kovalchuk / name: Maksym Kovalchuk (6a97e65, 1793c04)

@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@maksym-kovalchuk maksym-kovalchuk force-pushed the plugin-not-found-error-processing branch from 6b5b3a3 to a401ddc Compare April 8, 2025 11:25
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@maksym-kovalchuk maksym-kovalchuk force-pushed the plugin-not-found-error-processing branch from a401ddc to 726b3f7 Compare April 9, 2025 12:53
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@maksym-kovalchuk maksym-kovalchuk force-pushed the plugin-not-found-error-processing branch from 726b3f7 to b341e11 Compare April 9, 2025 14:18
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@maksym-kovalchuk maksym-kovalchuk force-pushed the plugin-not-found-error-processing branch from b341e11 to 99d5e4b Compare April 9, 2025 15:13
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@maksym-kovalchuk maksym-kovalchuk force-pushed the plugin-not-found-error-processing branch from 99d5e4b to da71522 Compare April 10, 2025 07:23
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@maksym-kovalchuk maksym-kovalchuk force-pushed the plugin-not-found-error-processing branch from da71522 to cedd723 Compare April 10, 2025 09:47
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@maksym-kovalchuk maksym-kovalchuk force-pushed the plugin-not-found-error-processing branch from cedd723 to 4e1db14 Compare April 10, 2025 13:26
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@maksym-kovalchuk maksym-kovalchuk force-pushed the plugin-not-found-error-processing branch from 4e1db14 to 05b8ffe Compare April 10, 2025 14:13
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

…ning log message instead of default error message

Avoid excess error messages for cases of race conditions when plugin was
removed right before the stage of its loading

Signed-off-by: Maksym Kovalchuk <[email protected]>
@maksym-kovalchuk maksym-kovalchuk force-pushed the plugin-not-found-error-processing branch from 05b8ffe to 1793c04 Compare April 10, 2025 14:27
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@qiluo-msft
Copy link
Contributor

/Semgrep

@qiluo-msft qiluo-msft closed this Jul 21, 2025
@qiluo-msft qiluo-msft reopened this Jul 21, 2025
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@qiluo-msft qiluo-msft enabled auto-merge (squash) July 21, 2025 18:59
@qiluo-msft qiluo-msft merged commit 8647356 into sonic-net:master Jul 21, 2025
8 checks passed
@mssonicbld
Copy link
Collaborator

Cherry-pick PR to 202505: #3992

YairRaviv pushed a commit to YairRaviv/sonic-utilities that referenced this pull request Jan 12, 2026
…_warning (sonic-net#3832)

There is the case of rare situation of race condition when plugin script file is removed right before the stage of its load via cli (e.g. in case of dynamic removing with app extensions). This situation has no impact but generate additional error messages to syslog

What I did
Add separate processing ModuleNotFoundError with log_warning messages to avoid excess ERR printing

How to verify it
It happens very rarely in nature but you can just try to remove plugin script manually right before its loading stage. I used this changes on top of my commit for testing:

diff --git a/utilities_common/util_base.py b/utilities_common/util_base.py
index 95098b7..c808b0a 100644
--- a/utilities_common/util_base.py
+++ b/utilities_common/util_base.py
@@ -2,6 +2,8 @@ import os
 import pkgutil
 import importlib

+import subprocess
+
 from sonic_py_common import logger

 # Constants ====================================================================
@@ -11,6 +13,7 @@ PDDF_SUPPORT_FILE = '/usr/share/sonic/platform/pddf_support'

 log = logger.Logger()

+plugin_path = "/usr/local/lib/python3.11/dist-packages/show/plugins/dhcp-relay.py"

 class UtilHelper(object):
     def __init__(self):
@@ -28,11 +31,15 @@ class UtilHelper(object):
                 continue
             log.log_debug('importing plugin: {}'.format(module_name))
             try:
+                if module_name == "show.plugins.dhcp-relay":
+                    subprocess.run(['mv', plugin_path, '/root/' ])
                 module = importlib.import_module(module_name)

             except ModuleNotFoundError as err:
                 log.log_warning('failed to import plugin {}: {}'.format(module_name, err),
                               also_print_to_console=True)
+                if module_name == "show.plugins.dhcp-relay":
+                    subprocess.run(['mv', '/root/dhcp-relay.py', plugin_path ])
                 continue

             except Exception as err:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants