gitlab-ci: serial close can leave an active read

So instead cancel the read first, and then close. Make sure the
serial-reading properly detects this cancelled condition under all
circumstances and exits.

Signed-off-by: Ilia Mirkin <imirkin@alum.mit.edu>
Reviewed-by: Emma Anholt <emma@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14033>
This commit is contained in:
Ilia Mirkin 2021-12-02 01:30:45 -05:00 committed by Marge Bot
parent 1abd6375c9
commit eb0b08ea1a
1 changed files with 8 additions and 9 deletions

View File

@ -28,7 +28,6 @@ import serial
import threading
import time
class SerialBuffer:
def __init__(self, dev, filename, prefix, timeout = None):
self.filename = filename
@ -36,7 +35,7 @@ class SerialBuffer:
if dev:
self.f = open(filename, "wb+")
self.serial = serial.Serial(dev, 115200, timeout=timeout if timeout else 10)
self.serial = serial.Serial(dev, 115200, timeout=timeout)
else:
self.f = open(filename, "rb")
self.serial = None
@ -63,9 +62,11 @@ class SerialBuffer:
def close(self):
self.closing = True
if self.serial:
self.serial.close()
self.serial.cancel_read()
self.read_thread.join()
self.lines_thread.join()
if self.serial:
self.serial.close()
# Thread that just reads the bytes from the serial device to try to keep from
# buffer overflowing it. If nothing is received in 1 minute, it finalizes.
@ -73,18 +74,16 @@ class SerialBuffer:
greet = "Serial thread reading from %s\n" % self.dev
self.byte_queue.put(greet.encode())
while True:
while not self.closing:
try:
b = self.serial.read()
if len(b) > 0:
self.byte_queue.put(b)
elif self.timeout:
self.byte_queue.put(self.sentinel)
if len(b) == 0:
break
self.byte_queue.put(b)
except Exception as err:
print(self.prefix + str(err))
self.byte_queue.put(self.sentinel)
break
self.byte_queue.put(self.sentinel)
# Thread that just reads the bytes from the file of serial output that some
# other process is appending to.