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 75E574402B; Tue, 14 May 2024 22:15:05 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C5476402DF; Tue, 14 May 2024 22:15:02 +0200 (CEST) Received: from mail-oo1-f101.google.com (mail-oo1-f101.google.com [209.85.161.101]) by mails.dpdk.org (Postfix) with ESMTP id 56AAC402DF for ; Tue, 14 May 2024 22:15:01 +0200 (CEST) Received: by mail-oo1-f101.google.com with SMTP id 006d021491bc7-5b2733389f9so3316725eaf.1 for ; Tue, 14 May 2024 13:15:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iol.unh.edu; s=unh-iol; t=1715717700; x=1716322500; 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=AHn+szi4mGLw/W8NcnSEVB7VaKey8Jt+M6Zt7nHd/VU=; b=Ckckt3X+QeBoxw9b05jdixp/QD90QQQc6/ymcx8YAArfktCCDVzLooiJE+DqE9q9YH o8zuNyTsM48TSrM5Tc955j0rPvHmLuS6gsN7UdLovC8ZigXJH5wyj6c4cEeWu4SzV6I4 Icq+DbPaV5E5BeQHijsW4usJlIRxdApcD0Jao= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715717700; x=1716322500; 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=AHn+szi4mGLw/W8NcnSEVB7VaKey8Jt+M6Zt7nHd/VU=; b=rGtVhLQw8iUO/sjgQcsSpCNlf8AzFQtTa+LBaJJmVlcj5bmS9FiX02UNiRM9g0fyTp gQOqb71bdXXSGqxREjAUDRtMA101zbvX7dH7DpbdxPGQZRKpWMooBqSjGPSRP8ecg7Rt OVDIGy2qFyPRLvS0PJHSgs5ToeqBJvCP1nCOVJ7HCKPrNoyk5YYz0f4bAMeIWo3lcefX rzys+guscXY7H8iy+AyO3eQ0j1jlLYXNl9rD7p5YsAF1gV/UoXguilPbUuYoUUAeySrU p2GAJft6G8xINW0qmmJHJnd7/V8UmE4fSYxaXiz3kUtTG8ORPchvUf7Qr4g2UXG4CSsC 96Ew== X-Gm-Message-State: AOJu0YzdSCEYEiCIBAsdL38VVLNnQ+KzyMSvsau/AusuAfMg/r+dSSZL b0jm05tPWrcPeBuCT0PJNGc7JdRXOwm6W7XO3+xfoGRDtDsNYdYh9YGoAWYykJ5Q/uzYCFokZey BtyIf/KS2pl6x/+aFlKgNz83sEXDOnr9NOJHBp5AC0hgdmdFv X-Google-Smtp-Source: AGHT+IHAAij4M2yoUPMP1Wb3BmATxh2vhSxywV5NtjwEiQnpj3uk5yvPyUsh0to/kanzIuAQHI6rr+eXIqeY X-Received: by 2002:a05:6359:7c1f:b0:193:fcb7:767c with SMTP id e5c5f4694b2df-193fcb7771fmr1117985655d.25.1715717700505; Tue, 14 May 2024 13:15:00 -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 6a1803df08f44-6a15f23f2b4sm7509496d6.49.2024.05.14.13.15.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 May 2024 13:15:00 -0700 (PDT) X-Relaying-Domain: iol.unh.edu Received: from iol.unh.edu (unknown [IPv6:2606:4100:3880:1271:90f9:1b64:f6e6:867f]) by postal.iol.unh.edu (Postfix) with ESMTP id 14F1B605C373; Tue, 14 May 2024 16:15:00 -0400 (EDT) From: jspewock@iol.unh.edu To: yoan.picchi@foss.arm.com, Honnappa.Nagarahalli@arm.com, paul.szczepanek@arm.com, juraj.linkes@pantheon.tech, probb@iol.unh.edu, Luca.Vizzarro@arm.com, wathsala.vithanage@arm.com, thomas@monjalon.net Cc: dev@dpdk.org, Jeremy Spewock Subject: [PATCH v1 1/4] dts: improve starting and stopping interactive shells Date: Tue, 14 May 2024 16:14:33 -0400 Message-ID: <20240514201436.2496-2-jspewock@iol.unh.edu> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240514201436.2496-1-jspewock@iol.unh.edu> References: <20240514201436.2496-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 | 4 +- 2 files changed, 45 insertions(+), 10 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..cb4642bf3d 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() + return super()._close() def get_capas_rxq( self, supported_capabilities: MutableSet, unsupported_capabilities: MutableSet -- 2.44.0