Bug 2085 fixed: added an async lock everytime a cmd is executed in the Helm conn 25/12225/5
authorPedro Escaleira <escaleira@av.it.pt>
Mon, 20 Jun 2022 14:40:43 +0000 (15:40 +0100)
committergarciadav <david.garcia@canonical.com>
Tue, 21 Jun 2022 08:34:13 +0000 (10:34 +0200)
Change-Id: I118ab7264cff8e51f098e9249fbb4d0872290058
Signed-off-by: Pedro Escaleira <escaleira@av.it.pt>
n2vc/k8s_helm_base_conn.py

index f0a049b..e494a76 100644 (file)
@@ -90,6 +90,9 @@ class K8sHelmBaseConnector(K8sConnector):
         if self._stable_repo_url == "None":
             self._stable_repo_url = None
 
         if self._stable_repo_url == "None":
             self._stable_repo_url = None
 
+        # Lock to avoid concurrent execution of helm commands
+        self.cmd_lock = asyncio.Lock()
+
     def _get_namespace(self, cluster_uuid: str) -> str:
         """
         Obtains the namespace used by the cluster with the uuid passed by argument
     def _get_namespace(self, cluster_uuid: str) -> str:
         """
         Obtains the namespace used by the cluster with the uuid passed by argument
@@ -1506,17 +1509,18 @@ class K8sHelmBaseConnector(K8sConnector):
             environ.update(env)
 
         try:
             environ.update(env)
 
         try:
-            process = await asyncio.create_subprocess_exec(
-                *command,
-                stdout=asyncio.subprocess.PIPE,
-                stderr=asyncio.subprocess.PIPE,
-                env=environ,
-            )
+            async with self.cmd_lock:
+                process = await asyncio.create_subprocess_exec(
+                    *command,
+                    stdout=asyncio.subprocess.PIPE,
+                    stderr=asyncio.subprocess.PIPE,
+                    env=environ,
+                )
 
 
-            # wait for command terminate
-            stdout, stderr = await process.communicate()
+                # wait for command terminate
+                stdout, stderr = await process.communicate()
 
 
-            return_code = process.returncode
+                return_code = process.returncode
 
             output = ""
             if stdout:
 
             output = ""
             if stdout:
@@ -1580,16 +1584,19 @@ class K8sHelmBaseConnector(K8sConnector):
             environ.update(env)
 
         try:
             environ.update(env)
 
         try:
-            read, write = os.pipe()
-            await asyncio.create_subprocess_exec(*command1, stdout=write, env=environ)
-            os.close(write)
-            process_2 = await asyncio.create_subprocess_exec(
-                *command2, stdin=read, stdout=asyncio.subprocess.PIPE, env=environ
-            )
-            os.close(read)
-            stdout, stderr = await process_2.communicate()
+            async with self.cmd_lock:
+                read, write = os.pipe()
+                await asyncio.create_subprocess_exec(
+                    *command1, stdout=write, env=environ
+                )
+                os.close(write)
+                process_2 = await asyncio.create_subprocess_exec(
+                    *command2, stdin=read, stdout=asyncio.subprocess.PIPE, env=environ
+                )
+                os.close(read)
+                stdout, stderr = await process_2.communicate()
 
 
-            return_code = process_2.returncode
+                return_code = process_2.returncode
 
             output = ""
             if stdout:
 
             output = ""
             if stdout: