From fdb24ef1399a32d48aeb9a7570076ff6a8031467 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 25 Jun 2017 04:39:08 +0000 Subject: [PATCH] coredevice: truncate overlong exception messages. If we have a really long one (megabytes) then this may exhaust the heap of the core device and crash it. --- artiq/coredevice/comm_kernel.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/artiq/coredevice/comm_kernel.py b/artiq/coredevice/comm_kernel.py index 66401f64e..9165b0062 100644 --- a/artiq/coredevice/comm_kernel.py +++ b/artiq/coredevice/comm_kernel.py @@ -494,6 +494,12 @@ class CommKernel: else: raise IOError("Unknown RPC value tag: {}".format(repr(tag))) + def _truncate_message(self, msg, limit=4096): + if len(msg) > limit: + return msg[0:limit] + "... (truncated)" + else: + return msg + def _serve_rpc(self, embedding_map): async = self._read_bool() service_id = self._read_int32() @@ -526,7 +532,7 @@ class CommKernel: if hasattr(exn, "artiq_core_exception"): exn = exn.artiq_core_exception self._write_string(exn.name) - self._write_string(exn.message) + self._write_string(self._truncate_message(exn.message)) for index in range(3): self._write_int64(exn.param[index]) @@ -543,8 +549,9 @@ class CommKernel: else: exn_id = embedding_map.store_object(exn_type) self._write_string("{}:{}.{}".format(exn_id, - exn_type.__module__, exn_type.__qualname__)) - self._write_string(str(exn)) + exn_type.__module__, + exn_type.__qualname__)) + self._write_string(self._truncate_message(str(exn))) for index in range(3): self._write_int64(0)