From e3507650dde85d1f6885e3a460f98c87bc34e75d Mon Sep 17 00:00:00 2001 From: tcezard Date: Wed, 27 Mar 2024 18:16:25 +0000 Subject: [PATCH 1/3] Ensure that the port forwarding command is running before returning the process --- ebi_eva_common_pyutils/network_utils.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ebi_eva_common_pyutils/network_utils.py b/ebi_eva_common_pyutils/network_utils.py index e4666a3..7ac6ffa 100644 --- a/ebi_eva_common_pyutils/network_utils.py +++ b/ebi_eva_common_pyutils/network_utils.py @@ -11,7 +11,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import time +from subprocess import Popen import requests import subprocess @@ -39,11 +40,18 @@ def get_available_local_port(try_starting_with_port): logger.error("Could not forward to any local port!") -def forward_remote_port_to_local_port(remote_host: str, remote_port: int, local_port: int) -> int: +def forward_remote_port_to_local_port(remote_host: str, remote_port: int, local_port: int) -> Popen: port_forward_command = 'ssh -N -L{0}:localhost:{1} {2}'.format(local_port, remote_port, remote_host) logger.info("Forwarding port to local port using command: " + port_forward_command) proc = subprocess.Popen(port_forward_command.split(" ")) - return proc.pid + time.sleep(5) + # Ensure that the process is still running + poll = proc.poll() + if poll is not None: + # The process already completed which mean it most likely crashed + logger.error(f'Port Forwarding {remote_host}:{remote_port} -> {local_port} failed!') + raise subprocess.CalledProcessError(proc.returncode, proc.args) + return proc @retry(exceptions=(ConnectionError, requests.RequestException), logger=logger, From d2fd29f958c955b6c218b86d120466ae96ea38ec Mon Sep 17 00:00:00 2001 From: tcezard Date: Thu, 28 Mar 2024 11:16:15 +0000 Subject: [PATCH 2/3] revert to only returning the process_id --- ebi_eva_common_pyutils/network_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ebi_eva_common_pyutils/network_utils.py b/ebi_eva_common_pyutils/network_utils.py index 7ac6ffa..c77f47a 100644 --- a/ebi_eva_common_pyutils/network_utils.py +++ b/ebi_eva_common_pyutils/network_utils.py @@ -40,7 +40,7 @@ def get_available_local_port(try_starting_with_port): logger.error("Could not forward to any local port!") -def forward_remote_port_to_local_port(remote_host: str, remote_port: int, local_port: int) -> Popen: +def forward_remote_port_to_local_port(remote_host: str, remote_port: int, local_port: int) -> int: port_forward_command = 'ssh -N -L{0}:localhost:{1} {2}'.format(local_port, remote_port, remote_host) logger.info("Forwarding port to local port using command: " + port_forward_command) proc = subprocess.Popen(port_forward_command.split(" ")) @@ -51,7 +51,7 @@ def forward_remote_port_to_local_port(remote_host: str, remote_port: int, local_ # The process already completed which mean it most likely crashed logger.error(f'Port Forwarding {remote_host}:{remote_port} -> {local_port} failed!') raise subprocess.CalledProcessError(proc.returncode, proc.args) - return proc + return proc.pid @retry(exceptions=(ConnectionError, requests.RequestException), logger=logger, From 8e6100f28540888681a764d494ab9a3b31d535a5 Mon Sep 17 00:00:00 2001 From: tcezard Date: Thu, 28 Mar 2024 11:17:27 +0000 Subject: [PATCH 3/3] remove unused import --- ebi_eva_common_pyutils/network_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ebi_eva_common_pyutils/network_utils.py b/ebi_eva_common_pyutils/network_utils.py index c77f47a..c3112b7 100644 --- a/ebi_eva_common_pyutils/network_utils.py +++ b/ebi_eva_common_pyutils/network_utils.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. import time -from subprocess import Popen import requests import subprocess