Skip to content

Commit 2cea585

Browse files
smrtosmmahadevan108
authored andcommitted
twisterlib: re-add the logic to drain the serial leftover
This logic was added before but seems to be missed during the twister refactoring. Some tests can cause serial leftover logs buffered somewhere. Such leftover can interfere with the next test case because the serial log monitoring thread is started before a board is flashed. And the monitoring thread can be fooled by such leftover logs and make incorrect judgement of the test result. A simple ser.flush() is not enough to eliminate such leftovers. So add explicit readline() to drain such logs which ensures a clean serial context for the case that follows. An example from reel board captured with this patch: leftover log of previous test: b'.287 seconds\r\n' leftover log of previous test: b' - SKIP - [...test_coredump_backend]... leftover log of previous test: b'\r\n' leftover log of previous test: b'------ TESTSUITE SUMMARY END ------\r\n' leftover log of previous test: b'\r\n' leftover log of previous test: b'=====================================... leftover log of previous test: b'RunID: 4e93757ad...53dcab9f0f5c6\r\n' leftover log of previous test: b'PROJECT EXECUTION SUCCESSFUL\r\n Signed-off-by: Ming Shao <[email protected]>
1 parent 5e756c1 commit 2cea585

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

scripts/pylib/twister/twisterlib/handlers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,19 @@ def monitor_serial(self, ser, halt_fileno, harness):
335335

336336
ser.flush()
337337

338+
# turns out the ser.flush() is not enough to clear serial leftover from last case
339+
# explicitly readline() can do it reliably
340+
old_timeout = ser.timeout
341+
# wait for 1s if no serial output
342+
ser.timeout = 1
343+
# or read 1000 lines at most
344+
# if the leftovers are more than 1000 lines, user should realize that once
345+
# saw the caught ones and fix it.
346+
leftover_lines = ser.readlines(1000)
347+
for line in leftover_lines:
348+
logger.debug(f"leftover log of previous test: {line}")
349+
ser.timeout = old_timeout
350+
338351
while ser.isOpen():
339352
readable, _, _ = select.select(readlist, [], [], self.timeout)
340353

0 commit comments

Comments
 (0)