From bb60a07e860794daa1ed0eb12b3280db72a7697a Mon Sep 17 00:00:00 2001 From: Hrushikesh Bhosale Date: Fri, 17 Apr 2026 14:36:58 +0530 Subject: [PATCH] fix(examples/system/ota/otatool): fix serial port contention in pytest The otatool pytest closes the serial port and then immediately launches otatool_example.py as a subprocess that re-opens the same port via esptool. This fails intermittently because pytest-embedded's QueueFeederThread may still hold the FD when close() returns, and the OS has not fully released the serial port by the time the subprocess tries to open it. Add a delay after serial close and a 3-attempt retry loop around the subprocess to handle transient serial port contention. (cherry picked from commit 926d5ee6ad0cbd0c15164c886c730ca1ed72f7e0) --- examples/system/ota/otatool/pytest_otatool.py | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/examples/system/ota/otatool/pytest_otatool.py b/examples/system/ota/otatool/pytest_otatool.py index 4f8fd2bd852..1656ea2fd7b 100644 --- a/examples/system/ota/otatool/pytest_otatool.py +++ b/examples/system/ota/otatool/pytest_otatool.py @@ -1,8 +1,10 @@ -# SPDX-FileCopyrightText: 2022-2024 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 @@ -16,6 +18,10 @@ def _real_test_func(dut: Dut) -> None: # Close connection to DUT dut.serial.proc.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) + script_path = os.path.join(str(os.getenv('IDF_PATH')), 'examples', 'system', 'ota', 'otatool', 'otatool_example.py') binary_path = '' @@ -23,7 +29,23 @@ def _real_test_func(dut: Dut) -> None: if 'otatool.bin' in flash_file[1]: binary_path = flash_file[1] break - subprocess.check_call([sys.executable, script_path, '--binary', binary_path]) + + # Retry the subprocess to handle transient serial port contention. + # The otatool_example.py subprocess 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]) + return + except subprocess.CalledProcessError as e: + last_err = e + logging.warning('otatool subprocess attempt %d/3 failed: %s', attempt + 1, e) + time.sleep(3) + + assert last_err is not None + raise last_err @pytest.mark.esp32