diff options
author | Tom Tromey <tromey@adacore.com> | 2024-01-10 12:51:10 -0700 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2024-01-22 08:54:27 -0700 |
commit | 46bdb641642d20307521f5db2e1f1c7dcc49d5a7 (patch) | |
tree | b110f2297893f23939972947289584d8081610be /gdb/python/lib | |
parent | 8f42049a8230baf871ccb66b4e607bd2ebcd99ab (diff) | |
download | binutils-46bdb641642d20307521f5db2e1f1c7dcc49d5a7.zip binutils-46bdb641642d20307521f5db2e1f1c7dcc49d5a7.tar.gz binutils-46bdb641642d20307521f5db2e1f1c7dcc49d5a7.tar.bz2 |
Handle EOF more gracefully in DAP
A user pointed out that gdb will print a Python exception when it gets
an EOF in DAP mode. And, it turns out that an EOF like this also
causes gdb not to exit. This is due to the refactoring that moved the
JSON reader to its own thread -- previously this caused an exception
to propagate and cause an exit, but now it just leaves the reader
hung.
This patch fixes these problems by arranging to handle EOF more
gracefully.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31217
Diffstat (limited to 'gdb/python/lib')
-rw-r--r-- | gdb/python/lib/gdb/dap/io.py | 54 | ||||
-rw-r--r-- | gdb/python/lib/gdb/dap/server.py | 7 |
2 files changed, 41 insertions, 20 deletions
diff --git a/gdb/python/lib/gdb/dap/io.py b/gdb/python/lib/gdb/dap/io.py index 611f0c2..5149eda 100644 --- a/gdb/python/lib/gdb/dap/io.py +++ b/gdb/python/lib/gdb/dap/io.py @@ -15,30 +15,44 @@ import json -from .startup import start_thread, send_gdb, log +from .startup import start_thread, send_gdb, log, log_stack, LogLevel def read_json(stream): """Read a JSON-RPC message from STREAM. - The decoded object is returned.""" - # First read and parse the header. - content_length = None - while True: - line = stream.readline() - line = line.strip() - if line == b"": - break - if line.startswith(b"Content-Length:"): - line = line[15:].strip() - content_length = int(line) - continue - log("IGNORED: <<<%s>>>" % line) - data = bytes() - while len(data) < content_length: - new_data = stream.read(content_length - len(data)) - data += new_data - result = json.loads(data) - return result + The decoded object is returned. + None is returned on EOF.""" + try: + # First read and parse the header. + content_length = None + while True: + line = stream.readline() + # If the line is empty, we hit EOF. + if len(line) == 0: + log("EOF") + return None + line = line.strip() + if line == b"": + break + if line.startswith(b"Content-Length:"): + line = line[15:].strip() + content_length = int(line) + continue + log("IGNORED: <<<%s>>>" % line) + data = bytes() + while len(data) < content_length: + new_data = stream.read(content_length - len(data)) + # Maybe we hit EOF. + if len(new_data) == 0: + log("EOF after reading the header") + return None + data += new_data + return json.loads(data) + except OSError: + # Reading can also possibly throw an exception. Treat this as + # EOF. + log_stack(LogLevel.FULL) + return None def start_json_writer(stream, queue): diff --git a/gdb/python/lib/gdb/dap/server.py b/gdb/python/lib/gdb/dap/server.py index e96c79d..7cc5a46 100644 --- a/gdb/python/lib/gdb/dap/server.py +++ b/gdb/python/lib/gdb/dap/server.py @@ -187,6 +187,8 @@ class Server: def _reader_thread(self): while True: cmd = read_json(self.in_stream) + if cmd is None: + break log("READ: <<<" + json.dumps(cmd) + ">>>") # Be extra paranoid about the form here. If anything is # missing, it will be put in the queue and then an error @@ -201,6 +203,8 @@ class Server: ): self.canceller.cancel(cmd["arguments"]["requestId"]) self.read_queue.put(cmd) + # When we hit EOF, signal it with None. + self.read_queue.put(None) @in_dap_thread def main_loop(self): @@ -212,6 +216,9 @@ class Server: start_thread("JSON reader", self._reader_thread) while not self.done: cmd = self.read_queue.get() + # A None value here means the reader hit EOF. + if cmd is None: + break result = self._handle_command(cmd) self._send_json(result) events = self.delayed_events |