teejusb / fsr

FSR code used for dance pads
GNU General Public License v3.0
62 stars 43 forks source link

When serial port is not found, server uses a lot of CPU, even after stopping main process #29

Closed joshhead closed 2 years ago

joshhead commented 2 years ago

Tested on macOS Big Sur 11.4

When the serial port is configured incorrectly or the device is not plugged in, I see an error like the following.

~/Documents/teejusb/fsr/webui $ yarn start-api
yarn run v1.22.17
$ cd server && venv/bin/python server.py
 * WebUI can be found at: http://192.168.1.67:5000
======== Running on http://0.0.0.0:5000 ========
(Press CTRL+C to quit)
Client connected
Found Profiles: ['']
Starting Read thread
Starting Write thread
Error opening serial: [Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0'
Traceback (most recent call last):
  File "/Users/jheadapohl/Documents/teejusb/fsr/webui/server/venv/lib/python3.9/site-packages/serial/serialposix.py", line 265, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyACM0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jheadapohl/Documents/teejusb/fsr/webui/server/server.py", line 179, in Open
    self.ser = serial.Serial(self.port, 115200, timeout=self.timeout)
  File "/Users/jheadapohl/Documents/teejusb/fsr/webui/server/venv/lib/python3.9/site-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/Users/jheadapohl/Documents/teejusb/fsr/webui/server/venv/lib/python3.9/site-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0'
Error opening serial: [Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0'
Traceback (most recent call last):
  File "/Users/jheadapohl/Documents/teejusb/fsr/webui/server/venv/lib/python3.9/site-packages/serial/serialposix.py", line 265, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyACM0'
...

This is fine and expected; it tells me the file for the serial port is missing. There are two problems though. First, the error is printed repeatedly as fast as the server process can go. Second, quitting the server with Ctrl+C does not stop the thread that is printing the errors, which makes the open terminal difficult to use and also makes my computer warmer (and noisier, as the fan ramps up). I can kill the leftover processes manually or close the terminal they are running in.

Ideally Ctrl+C should stop all the server processes, and separately it might be useful to add a delay when retrying the connection to the serial port.

joshhead commented 2 years ago

Fixed by #30