Blame SOURCES/wa-Fix-if-hangs.patch

60528a
From 12848c286008ef850bedb4552d4a8778f3164b00 Mon Sep 17 00:00:00 2001
60528a
From: Mohammed Gamal <mgamal@redhat.com>
60528a
Date: Thu, 2 Jun 2022 14:39:42 +0200
60528a
Subject: [PATCH] Fix if hangs (#2283)
60528a
60528a
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2092753
60528a
60528a
Signed-off-by: Laveesh Rohra <larohra@microsoft.com>
60528a
(cherry picked from commit 05cd6437ba90928788ef18c8b9fc8a6dbaf47c7d)
60528a
60528a
Signed-off-by: Mohammed Gamal <mgamal@redhat.com>
60528a
---
60528a
 azurelinuxagent/common/osutil/default.py | 26 ++++++++----------------
60528a
 azurelinuxagent/common/osutil/ubuntu.py  | 22 +++++++++-----------
60528a
 tests/common/osutil/test_default.py      | 11 +++-------
60528a
 3 files changed, 21 insertions(+), 38 deletions(-)
60528a
60528a
diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py
60528a
index 066e1431..820016c1 100644
60528a
--- a/azurelinuxagent/common/osutil/default.py
60528a
+++ b/azurelinuxagent/common/osutil/default.py
60528a
@@ -1163,25 +1163,15 @@ class DefaultOSUtil(object):
60528a
     def restart_if(self, ifname, retries=3, wait=5):
60528a
         retry_limit = retries + 1
60528a
         for attempt in range(1, retry_limit):
60528a
-            try:
60528a
-                shellutil.run_command(["ifdown", ifname])
60528a
-                shellutil.run_command(["ifup", ifname])
60528a
+            return_code = shellutil.run("ifdown {0} && ifup {0}".format(ifname), expected_errors=[1] if attempt < retries else [])
60528a
+            if return_code == 0:
60528a
                 return
60528a
-            except shellutil.CommandError as cmd_err:
60528a
-                
60528a
-                msg = "failed to restart {0}: returncode={1}\n[stdout]{2}\n\n[stderr]{3}\n"\
60528a
-                    .format(ifname, cmd_err.returncode, cmd_err.stdout, cmd_err.stderr)
60528a
-                
60528a
-                if cmd_err.returncode == 1:
60528a
-                    logger.info(msg)
60528a
-                else:
60528a
-                    logger.warn(msg)
60528a
-
60528a
-                if attempt < retry_limit:
60528a
-                    logger.info("retrying in {0} seconds".format(wait))
60528a
-                    time.sleep(wait)
60528a
-                else:
60528a
-                    logger.warn("exceeded restart retries")
60528a
+            logger.warn("failed to restart {0}: return code {1}".format(ifname, return_code))
60528a
+            if attempt < retry_limit:
60528a
+                logger.info("retrying in {0} seconds".format(wait))
60528a
+                time.sleep(wait)
60528a
+            else:
60528a
+                logger.warn("exceeded restart retries")
60528a
 
60528a
     def publish_hostname(self, hostname):
60528a
         self.set_dhcp_hostname(hostname)
60528a
diff --git a/azurelinuxagent/common/osutil/ubuntu.py b/azurelinuxagent/common/osutil/ubuntu.py
60528a
index 249e1120..5a21511c 100644
60528a
--- a/azurelinuxagent/common/osutil/ubuntu.py
60528a
+++ b/azurelinuxagent/common/osutil/ubuntu.py
60528a
@@ -142,19 +142,17 @@ class UbuntuOSUtil(Ubuntu16OSUtil):
60528a
         Restart an interface by bouncing the link. systemd-networkd observes
60528a
         this event, and forces a renew of DHCP.
60528a
         """
60528a
-        retry_limit=retries+1
60528a
+        retry_limit = retries+1
60528a
         for attempt in range(1, retry_limit):
60528a
-            try:
60528a
-                shellutil.run_command(["ip", "link", "set", ifname, "down"])
60528a
-                shellutil.run_command(["ip", "link", "set", ifname, "up"])
60528a
-
60528a
-            except shellutil.CommandError as cmd_err:
60528a
-                logger.warn("failed to restart {0}: return code {1}".format(ifname, cmd_err.returncode))
60528a
-                if attempt < retry_limit:
60528a
-                    logger.info("retrying in {0} seconds".format(wait))
60528a
-                    time.sleep(wait)
60528a
-                else:
60528a
-                    logger.warn("exceeded restart retries")
60528a
+            return_code = shellutil.run("ip link set {0} down && ip link set {0} up".format(ifname))
60528a
+            if return_code == 0:
60528a
+                return
60528a
+            logger.warn("failed to restart {0}: return code {1}".format(ifname, return_code))
60528a
+            if attempt < retry_limit:
60528a
+                logger.info("retrying in {0} seconds".format(wait))
60528a
+                time.sleep(wait)
60528a
+            else:
60528a
+                logger.warn("exceeded restart retries")
60528a
 
60528a
 
60528a
 class UbuntuSnappyOSUtil(Ubuntu14OSUtil):
60528a
diff --git a/tests/common/osutil/test_default.py b/tests/common/osutil/test_default.py
60528a
index 65d7ae0f..d6eae68f 100644
60528a
--- a/tests/common/osutil/test_default.py
60528a
+++ b/tests/common/osutil/test_default.py
60528a
@@ -49,20 +49,15 @@ class TestOSUtil(AgentTestCase):
60528a
         # setup
60528a
         retries = 3
60528a
         ifname = 'dummy'
60528a
-        with patch.object(shellutil, "run_command") as run_patch:
60528a
-            run_patch.side_effect = shellutil.CommandError("ifupdown dummy", 1, "", "")
60528a
+        with patch.object(shellutil, "run") as run_patch:
60528a
+            run_patch.return_value = 1
60528a
 
60528a
             # execute
60528a
             osutil.DefaultOSUtil.restart_if(osutil.DefaultOSUtil(), ifname=ifname, retries=retries, wait=0)
60528a
 
60528a
             # assert
60528a
             self.assertEqual(run_patch.call_count, retries)
60528a
-            cmd_queue = list(args[0] for (args, _) in run_patch.call_args_list)            
60528a
-            while cmd_queue:
60528a
-                self.assertEqual(cmd_queue.pop(0), ["ifdown", ifname])
60528a
-                # We don't expect the following command to be called because 'dummy' does
60528a
-                # not exist.
60528a
-                self.assertNotEqual(cmd_queue[0] if cmd_queue else None, ["ifup", ifname])
60528a
+            self.assertEqual(run_patch.call_args_list[0][0][0], 'ifdown {0} && ifup {0}'.format(ifname))
60528a
                 
60528a
     def test_get_dvd_device_success(self):
60528a
         with patch.object(os, 'listdir', return_value=['cpu', 'cdrom0']):
60528a
-- 
60528a
2.31.1
60528a