Serial port contention

I’m running into issues with what appears to be internal (to platformio) serial port contention on executing an upload.

Using the PlatformIO extension in VSCode on Windows11.

Attempting to upload to m5stack-atom, fails unexpectedly with:
could not open port 'COM9': PermissionError(13, 'Access is denied.', None, 5)

Monitor works as expected however:
... --- Miniterm on COM9 115200,8,N,1 ---...

Running various commands outside of VSCode works as expected:
➜ python .\ -p COM9 chip_id v3.1
Serial port COM9
Detecting chip type... ESP32

When there is an active Monitor running in VSCode, command outside of VSCode fails as expected, because the port is still open elsewhere.

Invoking in exactly the same way as it is invoked from the extension command: C:\Users\zzzz\.platformio\penv\Scripts\platformio.exe run --target upload --environment m5stack-atom
works correctly, e.g.:
C:\Users\zzzz\.platformio\penv\Scripts\python.exe .\ --chip esp32 --port COM9 --baud 1500000 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect 0x1000 C:\Users\zzzz\.platformio\packages\framework-arduinoespressif32\tools\sdk\bin\bootloader_dio_40m.bin 0x8000 C:\Repos\PIO\zzzz\.pio\build\m5stack-atom\partitions.bin 0xe000 C:\Users\zzzz\.platformio\packages\framework-arduinoespressif32\tools\partitions\boot_app0.bin 0x10000 C:\repos\PIO\zzzz\.pio\build\m5stack-atom\firmware.bin

Hoping someone can shed some light on this.

I’m wondering if platformio.exe opens the port before it invokes the script and forgets to close it.

Troubleshooting steps taken:

  • Machine restarted
  • Platform IO removed and reinstalled
  • Checked that no other apps were running that could be using the com port
  • Reinstalled the USB drivers

Additional information:
If I set upload_port = COM9: in platformio.ini, it works as expected (note the : on the end_
If I leave the upload_port out, it finds “COM9” - i.e. no : on the end, and unexpectedly fails

Using COM4: (i.e. less than COM9), also exhibits the same behaviour as above.

This is incredibly weird – putting a : at the end should not be required, and all my ESP32 uploads have worked perfectly without this. Please open an issue in Issues · platformio/platform-espressif32 · GitHub with your exact platformio.ini.