Skip to content

ESPLoader may fail to release serial port if caller did not use 'with' (ESPTOOL-1062) #14

@424778940z

Description

@424778940z

Operating System

Windows 10 LTSC 2021

Esptool Version

v4.8.1

Python Version

Python 3.11.2

Full Esptool Command Line that Was Run

N/A, used as library import

Esptool Output

Not from esptool but caller script

PS C:\Users\localadmin\Downloads\projectName\firmware\components\esp-cryptoauthlib\esp_cryptoauth_utility> python .\secure_cert_mfg.py --port com6 --target_chip esp32 --type --i2c-sda-pin 26 --i2c-scl-pin 25
C:\Users\localadmin\Downloads\projectName\firmware\components\esp-cryptoauthlib\esp_cryptoauth_utility\sample_bins\secure_cert_mfg_esp32.bin
Connecting...
.
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting...
Failed to get PID of a device on com6, using standard reset sequence.
..
Detecting chip type... ESP32
Chip detected
RAM boot...
Downloading 66312 bytes at 3ffb0000... done!
Downloading 520 bytes at 3ffc1950... done!
Downloading 169740 bytes at 40080000... done!
Downloading 20 bytes at 400a970c... done!
All segments done, executing at 40082668
Time required to load the app into the RAM = 10.692219972610474s
Traceback (most recent call last):
  File "C:\Users\localadmin\Downloads\projectName\firmware\components\esp-cryptoauthlib\esp_cryptoauth_utility\secure_cert_mfg.py", line 245, in <module>
    main()
  File "C:\Users\localadmin\Downloads\projectName\firmware\components\esp-cryptoauthlib\esp_cryptoauth_utility\secure_cert_mfg.py", line 125, in main
    init_mfg = hs.serial.cmd_interpreter(port=args.port)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\localadmin\Downloads\projectName\firmware\components\esp-cryptoauthlib\esp_cryptoauth_utility\helper_scripts\serial.py", line 43, in __init__    
    self.port.open()
  File "C:\_home\_dev\.espressif\python_env\idf5.4_py3.11_env\Lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'com6': PermissionError(13, '拒绝访问。', None, 5)

What is the Expected Behaviour?

Still not from esptool but caller script

PS C:\Users\localadmin\Downloads\projectName\firmware\components\esp-cryptoauthlib\esp_cryptoauth_utility> python .\secure_cert_mfg.py --port COM6 --target_chip esp32 --type --i2c-sda-pin 26 --i2c-scl-pin 25
C:\Users\localadmin\Downloads\projectName\firmware\components\esp-cryptoauthlib\esp_cryptoauth_utility\sample_bins\secure_cert_mfg_esp32.bin
Connecting....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting....
Detecting chip type... ESP32
Chip detected
RAM boot...
Downloading 66312 bytes at 3ffb0000... done!
Downloading 520 bytes at 3ffc1950... done!
Downloading 169740 bytes at 40080000... done!
Downloading 20 bytes at 400a970c... done!
All segments done, executing at 40082668
Time required to load the app into the RAM = 10.68584942817688s
Wait for init
2nd stage bootloader

I (10910) boot: compile time Jul 10 2023 19:50:59

I (10911) boot: Multicore bootloader

I (10915) boot.esp32: SPI Speed : 40MHz

I (10920) boot.esp32: SPI Mode : SLOW READ

More Information

The issue is on this line

Serial port is only released if __exit__() is called, but this function is for contex manager api in python, which seems won't be called if caller use yyy = xxx.open() instead of with xxx.open() as yyy: , thus the serial port being held and causing anything after it to fail

Caller is here, which is also ESP provided code

Append esp.__exit__(None,None,None) to load_app_stub() to force it call ESPLoader.__exit__() has been tested works as a workaround, but proper fix should be implemented

Other Steps to Reproduce

follow this document setup a basic venv with requirements.txt, then run any command from secure_cert_mfg.py will cause the error

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions