servo: handle exceptions in UART dump()

Sometimes UART streams are 'not_applicable'. If not properly handled,
this lead to exceptions during Servo.close() and thus background ssh
processes not teared down.

BUG=b:138914412
TEST=manual testing

Change-Id: Iefdff50f3584d31e0d1cef6e3f1c22ef298b90b6
Reviewed-on: https://chromium-review.googlesource.com/1779761
Tested-by: Kuang-che Wu <kcwu@chromium.org>
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Chung-yih Wang <cywang@google.com>
diff --git a/server/cros/servo/servo.py b/server/cros/servo/servo.py
index 8098263..92accc8 100644
--- a/server/cros/servo/servo.py
+++ b/server/cros/servo/servo.py
@@ -205,11 +205,19 @@
                 logging.warn('Failed to get UART log for %s: %s', stream, err)
                 continue
 
+            if content == 'not_applicable':
+                logging.warn('%s is not applicable', stream)
+                continue
+
             # The UART stream may contain non-printable characters, and servo
             # returns it in string representation. We use `ast.leteral_eval`
             # to revert it back.
             with open(logfile_fullname, 'a') as fd:
-                fd.write(ast.literal_eval(content))
+                try:
+                    fd.write(ast.literal_eval(content))
+                except ValueError:
+                    logging.exception('Invalid value for %s: %r', stream,
+                                      content)
 
     def stop_capture(self):
         """Stop capturing UART streams."""