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 9556B44164; Wed, 5 Jun 2024 23:32:32 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A2AE440687; Wed, 5 Jun 2024 23:32:31 +0200 (CEST) Received: from mail-ot1-f104.google.com (mail-ot1-f104.google.com [209.85.210.104]) by mails.dpdk.org (Postfix) with ESMTP id 50AEF402EA for ; Wed, 5 Jun 2024 23:32:30 +0200 (CEST) Received: by mail-ot1-f104.google.com with SMTP id 46e09a7af769-6f945b242easo224555a34.1 for ; Wed, 05 Jun 2024 14:32:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iol.unh.edu; s=unh-iol; t=1717623149; x=1718227949; 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=qKK3yXHEmhfP5QqUaKRInMFIrxjClUtAezEGlk2K12M=; b=VVuoV/y1C2naAXePOhOTNtr6JYwofKkmQxe2wo41EOeeHP+K819NLiN0vn6zamd++j PS5Qa/C9gyjej98yfh52SvRfprwpOVTJfss1f1Ye2YSMrhiWVxfNdZ4J9QRgtndslf7T HiTHFnWohTbmfxq1qYKrHiofBJpstfXqMXWM8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717623149; x=1718227949; 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=qKK3yXHEmhfP5QqUaKRInMFIrxjClUtAezEGlk2K12M=; b=J9bDnz4FZ2H0yAIEA//liJZ/PaRPieVRl9rnn0ysV8x9Z7zQoOIYxWtIYusUQ2kSOf PSciA8hMozXci/3U/PpbPKh5jrMCoH6lgp+VtT1hMDTw3M4ZUMInnmGBILB6Wv7xfqLk U2LXlzWJEJ3c8HFRmN2Ro2qXR6smq+PgQbvc6pHMOSzpOb3wxDstCcv2mFQkh8MtyDf2 TpPLsf5A4hIUYeTmm+ZRsnPJ5NFMm9zrDwd4FuhNv7JtkzFn1sw5jtral9j/qN/SdUyI PewnTTDO79Z5nSL4cFG4A+0S5eoK2JM6QRCOjxeMZ3J7EgabWJ93PS9mCpOl91JfP9b+ xQaA== X-Gm-Message-State: AOJu0YyYHs3sKnfL5GaEq/lkV+0IOmyqUGeL6sj9QNW87STnytA1AnQk frUXVunI1jMOXgELPFONxy3MJNankQgDAXcXiS5nbEw7mgGnMnsYWxNsotlRXeHCViAdzyvriIy S/WSNe55zKrm95Ixx9NMdcCL1E/W/b+K8Nr6LKV4LCtvwyOMj X-Google-Smtp-Source: AGHT+IF2yWi4Zto8E2HFQzr47AcxS2I0X2GrsTVC20dDb0fMOJje2SJitPwtAE2hPORhmI4T7TJuyzELUx91 X-Received: by 2002:a05:6870:9a23:b0:250:6aa0:7c8e with SMTP id 586e51a60fabf-25122b08365mr3268087fac.53.1717623149432; Wed, 05 Jun 2024 14:32:29 -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 586e51a60fabf-25447e6d0f6sm4327fac.24.2024.06.05.14.32.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2024 14:32:29 -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 50678605C380; Wed, 5 Jun 2024 17:32:28 -0400 (EDT) From: jspewock@iol.unh.edu To: Honnappa.Nagarahalli@arm.com, probb@iol.unh.edu, paul.szczepanek@arm.com, juraj.linkes@pantheon.tech, thomas@monjalon.net, wathsala.vithanage@arm.com, npratte@iol.unh.edu, yoan.picchi@foss.arm.com, Luca.Vizzarro@arm.com Cc: dev@dpdk.org, Jeremy Spewock Subject: [PATCH v3 1/4] dts: improve starting and stopping interactive shells Date: Wed, 5 Jun 2024 17:31:45 -0400 Message-ID: <20240605213148.21371-2-jspewock@iol.unh.edu> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240605213148.21371-1-jspewock@iol.unh.edu> References: <20240514201436.2496-1-jspewock@iol.unh.edu> <20240605213148.21371-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..921c73d9df 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(5) 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