From 2b86e51026cd39e7c0acc20eceaddc13b67b9977 Mon Sep 17 00:00:00 2001 From: vdahiya12 <67608553+vdahiya12@users.noreply.github.com> Date: Sun, 19 Jul 2020 20:58:52 -0700 Subject: [PATCH] [daemon_base] fix to not reregister signal handler (#4998) * [daemon_base] fix to not reregister signal handler -src/sonic-daemon-base/sonic_daemon_base/daemon_base.py Problem: Currently all daemons inherit from daemon_base class, and for signal handling functionality they register the signal_handler() by overriding the siganl_handler() in daemon_base by their own implmentation. But some sonic_platform instances also can invoke the daemon_base constructor while trying to instantiate the common utilities for example platform_chassis = sonic_platform.platform.Platform().get_chassis() This will cause the re registration of signal_handler which will cause base class signal_handler() to be invoked when the daemon gets a signal, whereas their own signal_handler should have been invoked. Fix: We only register the siganl_handler once, and if signal_handler has been registered, not re register it. Signed-off-by: vaibhav-dahiya * [daemon_base] fix to not reregister signal handler Signed-off-by: vaibhav-dahiya --- .../sonic_daemon_base/daemon_base.py | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/sonic-daemon-base/sonic_daemon_base/daemon_base.py b/src/sonic-daemon-base/sonic_daemon_base/daemon_base.py index 205bc9e57f..2debed5227 100644 --- a/src/sonic-daemon-base/sonic_daemon_base/daemon_base.py +++ b/src/sonic-daemon-base/sonic_daemon_base/daemon_base.py @@ -96,9 +96,25 @@ class Logger(object): class DaemonBase(object): def __init__(self): # Register our signal handlers - signal.signal(signal.SIGHUP, self.signal_handler) - signal.signal(signal.SIGINT, self.signal_handler) - signal.signal(signal.SIGTERM, self.signal_handler) + '''all daemons inherit from daemon_base class, and for + signal handling functionality they register the signal_handler() by + overriding the siganl_handler() in daemon_base by their own + implmentation. + But some sonic_platform instances also can invoke the daemon_base + constructor while trying to instantiate the common utilities + for example + platform_chassis = sonic_platform.platform.Platform().get_chassis() + This will cause the re registration of signal_handler which will + cause base class signal_handler() to be invoked when the daemon + gets a signal, whereas the derived class signal_handler should have + been invoked. The if checks will not allow the re registration + of signal handler ''' + if not signal.getsignal(signal.SIGHUP): + signal.signal(signal.SIGHUP, self.signal_handler) + if not signal.getsignal(signal.SIGINT): + signal.signal(signal.SIGINT, self.signal_handler) + if not signal.getsignal(signal.SIGTERM): + signal.signal(signal.SIGTERM, self.signal_handler) # Signal handler def signal_handler(self, sig, frame):