espressif / esptool

Espressif SoC serial bootloader utility
https://docs.espressif.com/projects/esptool
GNU General Public License v2.0
5.6k stars 1.39k forks source link

Use PySerial locking so the serial port isn't used by other programs (ESPTOOL-792) #946

Closed redfast00 closed 10 months ago

redfast00 commented 10 months ago

Is your feature request related to a problem?

I'm always frustrated when I try to flash a chip using esptool, and I accidentally left a serial console open somewhere else, causing esptool to behave weirdly (since multiple programs are using the same serial port, some of the data on the serial port ends up in esptool and the other part end up in the serial console).

Describe the solution you'd like

This can be avoided by advisory locking the serial port; in pyserial, this can be done with https://github.com/pyserial/pyserial/pull/206. A lot of programs already do this, but since this is advisory locking, it only helps if all programs you're using check the lock. See also https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=734086

A one-line fix would be to add under this line https://github.com/espressif/esptool/blob/master/esptool/loader.py#L303 self._port.exclusive = True. If you want to do something fancier, you can also catch the error, and maybe even display the program that is using it. For me, just seeing 'this port is already in use by another program' would already be useful.

Describe alternatives you've considered

No response

Additional context

This is on Linux; I don't think locking in pyserial is implemented in Windows, so it should not cause any issues there.

dobairoland commented 10 months ago

Thank you for letting us know about this.

redfast00 commented 10 months ago

Thanks for implementing it :)