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:
@@ -28,7 +28,6 @@ import serial
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
class SerialBuffer:
|
class SerialBuffer:
|
||||||
def __init__(self, dev, filename, prefix, timeout = None):
|
def __init__(self, dev, filename, prefix, timeout = None):
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
@@ -36,7 +35,7 @@ class SerialBuffer:
|
|||||||
|
|
||||||
if dev:
|
if dev:
|
||||||
self.f = open(filename, "wb+")
|
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:
|
else:
|
||||||
self.f = open(filename, "rb")
|
self.f = open(filename, "rb")
|
||||||
self.serial = None
|
self.serial = None
|
||||||
@@ -63,9 +62,11 @@ class SerialBuffer:
|
|||||||
def close(self):
|
def close(self):
|
||||||
self.closing = True
|
self.closing = True
|
||||||
if self.serial:
|
if self.serial:
|
||||||
self.serial.close()
|
self.serial.cancel_read()
|
||||||
self.read_thread.join()
|
self.read_thread.join()
|
||||||
self.lines_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
|
# 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.
|
# 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
|
greet = "Serial thread reading from %s\n" % self.dev
|
||||||
self.byte_queue.put(greet.encode())
|
self.byte_queue.put(greet.encode())
|
||||||
|
|
||||||
while True:
|
while not self.closing:
|
||||||
try:
|
try:
|
||||||
b = self.serial.read()
|
b = self.serial.read()
|
||||||
if len(b) > 0:
|
if len(b) == 0:
|
||||||
self.byte_queue.put(b)
|
|
||||||
elif self.timeout:
|
|
||||||
self.byte_queue.put(self.sentinel)
|
|
||||||
break
|
break
|
||||||
|
self.byte_queue.put(b)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(self.prefix + str(err))
|
print(self.prefix + str(err))
|
||||||
self.byte_queue.put(self.sentinel)
|
|
||||||
break
|
break
|
||||||
|
self.byte_queue.put(self.sentinel)
|
||||||
|
|
||||||
# Thread that just reads the bytes from the file of serial output that some
|
# Thread that just reads the bytes from the file of serial output that some
|
||||||
# other process is appending to.
|
# other process is appending to.
|
||||||
|
Reference in New Issue
Block a user