From ce6c4b89404fb11d61d0deffc1891f5d68c4b0b3 Mon Sep 17 00:00:00 2001 From: "sonika.rathi" Date: Tue, 7 Jul 2026 09:41:10 +0200 Subject: [PATCH] fix(storage): stabilize parttool pytest serial handoff --- .../parttool/pytest_parttool_example.py | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/examples/storage/parttool/pytest_parttool_example.py b/examples/storage/parttool/pytest_parttool_example.py index 567329a4092..999c6ac4552 100644 --- a/examples/storage/parttool/pytest_parttool_example.py +++ b/examples/storage/parttool/pytest_parttool_example.py @@ -1,8 +1,10 @@ -# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 +import logging import os import subprocess import sys +import time import pytest from pytest_embedded import Dut @@ -19,12 +21,32 @@ def test_examples_parttool(dut: Dut) -> None: # Close connection to DUT dut.serial.close() + # Allow the OS to fully release the serial port. pytest-embedded's + # QueueFeederThread may still hold the port FD when close() returns. + time.sleep(2) + # Run the example python script idf_path = os.getenv('IDF_PATH') assert idf_path is not None script_path = os.path.join(idf_path, 'examples', 'storage', 'parttool', 'parttool_example.py') binary_path = os.path.join(dut.app.binary_path, 'parttool.bin') - subprocess.check_call([sys.executable, script_path, '--binary', binary_path, '--port', dut.serial.port]) + + # Retry the subprocess to handle transient serial port contention. + # parttool_example.py opens the serial port independently via esptool, + # and may fail if pytest-embedded's QueueFeederThread has not fully + # released the port file descriptor yet. + last_err = None + for attempt in range(3): + try: + subprocess.check_call([sys.executable, script_path, '--binary', binary_path, '--port', dut.serial.port]) + break + except subprocess.CalledProcessError as e: + last_err = e + logging.warning('parttool subprocess attempt %d/3 failed: %s', attempt + 1, e) + time.sleep(3) + else: + assert last_err is not None + raise last_err # following tests check the external interface (parsing) of the parttool commands with open('custom.bin', 'wb') as f: