From 702d821aa3cd37a342ad184cad2e6da516935b21 Mon Sep 17 00:00:00 2001 From: horw Date: Tue, 8 Oct 2024 21:43:17 +0800 Subject: [PATCH] fix: close serial port when exception --- .../pytest_embedded_serial/serial.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pytest-embedded-serial/pytest_embedded_serial/serial.py b/pytest-embedded-serial/pytest_embedded_serial/serial.py index 99c5a818..2398bed7 100644 --- a/pytest-embedded-serial/pytest_embedded_serial/serial.py +++ b/pytest-embedded-serial/pytest_embedded_serial/serial.py @@ -90,14 +90,18 @@ def __init__( self.proc = pyserial.serial_for_url(self.port, **port_config) self.ports_to_occupy.append(self.port) - self._post_init() - self._start() + try: + self._post_init() + self._start() - self._finalize_init() - if not stop_after_init: - self.start_redirect_thread() - else: + self._finalize_init() + if not stop_after_init: + self.start_redirect_thread() + else: + self.close() + except Exception as e: self.close() + raise e def start_redirect_thread(self) -> None: if self._redirect_thread and self._redirect_thread.is_alive():