diff --git a/scripts/west_commands/runners/core.py b/scripts/west_commands/runners/core.py index cf01d2abf45380..20e4b72a8c3884 100644 --- a/scripts/west_commands/runners/core.py +++ b/scripts/west_commands/runners/core.py @@ -920,22 +920,27 @@ def ensure_output(self, output_type: str) -> None: # RuntimeError avoids a stack trace saved in run_common. raise RuntimeError(err) - def run_telnet_client(self, host: str, port: int) -> None: + def run_telnet_client(self, host: str, port: int, active_sock=None) -> None: ''' Run a telnet client for user interaction. ''' - # If a `nc` command is available, run it, as it will provide the best support for - # CONFIG_SHELL_VT100_COMMANDS etc. - if shutil.which('nc') is not None: + # If the caller passed in an active socket, use that + if active_sock is not None: + sock = active_sock + elif shutil.which('nc') is not None: + # If a `nc` command is available, run it, as it will provide the + # best support for CONFIG_SHELL_VT100_COMMANDS etc. client_cmd = ['nc', host, str(port)] # Note: netcat (nc) does not handle sigint, so cannot use run_client() self.check_call(client_cmd) return + else: + # Start a new socket connection + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((host, port)) # Otherwise, use a pure python implementation. This will work well for logging, # but input is line based only. - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((host, port)) sel = selectors.DefaultSelector() sel.register(sys.stdin, selectors.EVENT_READ) sel.register(sock, selectors.EVENT_READ) diff --git a/scripts/west_commands/runners/jlink.py b/scripts/west_commands/runners/jlink.py index f2c3bc21d9094e..00438ba6e5dd42 100644 --- a/scripts/west_commands/runners/jlink.py +++ b/scripts/west_commands/runners/jlink.py @@ -330,9 +330,7 @@ def do_run(self, command, **kwargs): break except ConnectionRefusedError: time.sleep(0.1) - sock.shutdown(socket.SHUT_RDWR) - time.sleep(0.1) - self.run_telnet_client('localhost', self.rtt_port) + self.run_telnet_client('localhost', self.rtt_port, sock) except Exception as e: self.logger.error(e) finally: