From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 063A944114; Thu, 30 May 2024 18:37:09 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2D85440EAB; Thu, 30 May 2024 18:37:08 +0200 (CEST) Received: from mail-il1-f231.google.com (mail-il1-f231.google.com [209.85.166.231]) by mails.dpdk.org (Postfix) with ESMTP id 8B3BE406FF for ; Thu, 30 May 2024 18:37:06 +0200 (CEST) Received: by mail-il1-f231.google.com with SMTP id e9e14a558f8ab-36dd56cf5f5so4099355ab.3 for ; Thu, 30 May 2024 09:37:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iol.unh.edu; s=unh-iol; t=1717087026; x=1717691826; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GBurkH2kKyhzt9aCUF7CgX6dwDd+CitxwHFGY572B/o=; b=PRWumo6BpJKFehZKIgWO6gC+cYVO0F+EPQ1NxAV4MIPgtyegbnVo1zipyG7OuaKIsB +5Pr4BY1RNWM+9Mb3VDX0EW0q2WJlYBTC88UeBORsXkp2h1M5POU7KnOOzD++yu7Ohv1 E7+fznUCRAxXCPjfLjFrnE2/hu64YtFlFxgl8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717087026; x=1717691826; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GBurkH2kKyhzt9aCUF7CgX6dwDd+CitxwHFGY572B/o=; b=c8uUp2jwrrrvF4nLAw8glS5wQroo4HsA6WITt9hk2ukF8Ls+8PXGxn9aJNltL9xRHW iFClEs7p/uIzhKfvwpzGM7ZRTCLQZpeuGpYynkpjEw2Pu7FgySf66I3CgWtQ37mD/15y 4bTA6q6azVl6vWvFmlz+X5CFJP1ZN/bH/9cI45gvHNqD7LX8BhN5sMHZDjOcu2xr9DXY ywrXpQxauEEVXJkvUZpxYRUZvLJ71AyOto99RRiVfDKW9rE2mOgPv6v8Q4QEb3CWGw9D ibU+QOF+J6GdfL8qWiHThKk9DVGMB1rJ9nmU3c7KNBu/xWbFFeW4yM1fIgK441WzODGq bEiA== X-Gm-Message-State: AOJu0Yxrz5ySgf6MSYoksmZwSomI40XPQme8nSR7wefZ1jcnfR+T8Jnb D3h0DhddMJ/3BswD1OxKPSrV/qRGZgCs8RHRIjkK+k3bF2Dv6VFMsZJN3qiRdIV9t4U3A3lF39H v1pgM5Nr3RwGP8pgzvn8qgKGCV7Css5o55h91Ji3gc0fXSxEB X-Google-Smtp-Source: AGHT+IHEGTVidxvwZtZPfeGr7znUysY198t0t1cuufDS/NStCAKcJkBLA3pgisk70SGzmDj8mA34gA3yvS3i X-Received: by 2002:a05:6e02:1986:b0:36c:42a0:7abb with SMTP id e9e14a558f8ab-3747e00f570mr27936505ab.31.1717087025870; Thu, 30 May 2024 09:37:05 -0700 (PDT) Received: from postal.iol.unh.edu (postal.iol.unh.edu. [2606:4100:3880:1234::84]) by smtp-relay.gmail.com with ESMTPS id e9e14a558f8ab-3748a35d858sm33105ab.41.2024.05.30.09.37.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 May 2024 09:37:05 -0700 (PDT) X-Relaying-Domain: iol.unh.edu Received: from iol.unh.edu (unknown [IPv6:2606:4100:3880:1257::1083]) by postal.iol.unh.edu (Postfix) with ESMTP id C9202605C373; Thu, 30 May 2024 12:37:04 -0400 (EDT) From: jspewock@iol.unh.edu To: juraj.linkes@pantheon.tech, paul.szczepanek@arm.com, wathsala.vithanage@arm.com, Honnappa.Nagarahalli@arm.com, probb@iol.unh.edu, yoan.picchi@foss.arm.com, Luca.Vizzarro@arm.com, npratte@iol.unh.edu, thomas@monjalon.net Cc: dev@dpdk.org, Jeremy Spewock Subject: [PATCH v2 1/4] dts: improve starting and stopping interactive shells Date: Thu, 30 May 2024 12:33:36 -0400 Message-ID: <20240530163339.14566-2-jspewock@iol.unh.edu> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240530163339.14566-1-jspewock@iol.unh.edu> References: <20240514201436.2496-1-jspewock@iol.unh.edu> <20240530163339.14566-1-jspewock@iol.unh.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Jeremy Spewock The InteractiveShell class currently relies on being cleaned up and shutdown at the time of garbage collection, but this cleanup of the class does no verification that the session is still running prior to cleanup. So, if a user were to call this method themselves prior to garbage collection, it would be called twice and throw an exception when the desired behavior is to do nothing since the session is already cleaned up. This is solved by using a weakref and a finalize class which achieves the same result of calling the method at garbage collection, but also ensures that it is called exactly once. Additionally, this fixes issues regarding starting a primary DPDK application while another is still cleaning up via a retry when starting interactive shells. It also adds catch for attempting to send a command to an interactive shell that is not running to create a more descriptive error message. Signed-off-by: Jeremy Spewock --- .../remote_session/interactive_shell.py | 51 ++++++++++++++++--- dts/framework/remote_session/testpmd_shell.py | 6 +-- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/dts/framework/remote_session/interactive_shell.py b/dts/framework/remote_session/interactive_shell.py index 5cfe202e15..d1a9d8a6d2 100644 --- a/dts/framework/remote_session/interactive_shell.py +++ b/dts/framework/remote_session/interactive_shell.py @@ -14,12 +14,14 @@ environment variable configure the timeout of getting the output from command execution. """ +import weakref from abc import ABC from pathlib import PurePath from typing import Callable, ClassVar from paramiko import Channel, SSHClient, channel # type: ignore[import] +from framework.exception import InteractiveCommandExecutionError from framework.logger import DTSLogger from framework.settings import SETTINGS @@ -32,6 +34,10 @@ class InteractiveShell(ABC): and collecting input until reaching a certain prompt. All interactive applications will use the same SSH connection, but each will create their own channel on that session. + + Attributes: + is_started: :data:`True` if the application has started successfully, :data:`False` + otherwise. """ _interactive_session: SSHClient @@ -41,6 +47,7 @@ class InteractiveShell(ABC): _logger: DTSLogger _timeout: float _app_args: str + _finalizer: weakref.finalize #: Prompt to expect at the end of output when sending a command. #: This is often overridden by subclasses. @@ -58,6 +65,8 @@ class InteractiveShell(ABC): #: for DPDK on the node will be prepended to the path to the executable. dpdk_app: ClassVar[bool] = False + is_started: bool = False + def __init__( self, interactive_session: SSHClient, @@ -93,17 +102,39 @@ def __init__( def _start_application(self, get_privileged_command: Callable[[str], str] | None) -> None: """Starts a new interactive application based on the path to the app. - This method is often overridden by subclasses as their process for - starting may look different. + This method is often overridden by subclasses as their process for starting may look + different. Initialization of the shell on the host can be retried up to 5 times. This is + done because some DPDK applications need slightly more time after exiting their script to + clean up EAL before others can start. + + When the application is started we also bind a class for finalization to this instance of + the shell to ensure proper cleanup of the application. Args: get_privileged_command: A function (but could be any callable) that produces the version of the command with elevated privileges. """ + self._finalizer = weakref.finalize(self, self._close) + max_retries = 5 + self._ssh_channel.settimeout(1) start_command = f"{self.path} {self._app_args}" if get_privileged_command is not None: start_command = get_privileged_command(start_command) - self.send_command(start_command) + self.is_started = True + for retry in range(max_retries): + try: + self.send_command(start_command) + break + except TimeoutError: + self._logger.info( + "Interactive shell failed to start, retrying... " + f"({retry+1} out of {max_retries})" + ) + else: + self._ssh_channel.settimeout(self._timeout) + self.is_started = False # update state on failure to start + raise InteractiveCommandExecutionError("Failed to start application.") + self._ssh_channel.settimeout(self._timeout) def send_command(self, command: str, prompt: str | None = None) -> str: """Send `command` and get all output before the expected ending string. @@ -125,6 +156,10 @@ def send_command(self, command: str, prompt: str | None = None) -> str: Returns: All output in the buffer before expected string. """ + if not self.is_started: + raise InteractiveCommandExecutionError( + f"Cannot send command {command} to application because the shell is not running." + ) self._logger.info(f"Sending: '{command}'") if prompt is None: prompt = self._default_prompt @@ -140,11 +175,11 @@ def send_command(self, command: str, prompt: str | None = None) -> str: self._logger.debug(f"Got output: {out}") return out - def close(self) -> None: - """Properly free all resources.""" + def _close(self) -> None: + self.is_started = False self._stdin.close() self._ssh_channel.close() - def __del__(self) -> None: - """Make sure the session is properly closed before deleting the object.""" - self.close() + def close(self) -> None: + """Properly free all resources.""" + self._finalizer() diff --git a/dts/framework/remote_session/testpmd_shell.py b/dts/framework/remote_session/testpmd_shell.py index f6783af621..284412e82c 100644 --- a/dts/framework/remote_session/testpmd_shell.py +++ b/dts/framework/remote_session/testpmd_shell.py @@ -227,10 +227,10 @@ def set_forward_mode(self, mode: TestPmdForwardingModes, verify: bool = True): f"Test pmd failed to set fwd mode to {mode.value}" ) - def close(self) -> None: + def _close(self) -> None: """Overrides :meth:`~.interactive_shell.close`.""" - self.send_command("quit", "") - return super().close() + self.send_command("quit", "Bye...") + return super()._close() def get_capas_rxq( self, supported_capabilities: MutableSet, unsupported_capabilities: MutableSet -- 2.45.1