https://github.com/piyushjaiswal98 updated https://github.com/llvm/llvm-project/pull/155335
>From 443c2b4c983399fce0ac6805abfa7ca2fffd1322 Mon Sep 17 00:00:00 2001 From: Piyush Jaiswal <piyushj...@meta.com> Date: Mon, 25 Aug 2025 17:31:45 -0700 Subject: [PATCH 1/3] Improving lldbdap server error diagnosability and adding exception handling --- .../test/tools/lldb-dap/lldbdap_testcase.py | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py index c23b2e73fb45e..8691a87b6fa87 100644 --- a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py @@ -466,8 +466,15 @@ def attach( # if we throw an exception during the test case. def cleanup(): if disconnectAutomatically: - self.dap_server.request_disconnect(terminateDebuggee=True) - self.dap_server.terminate() + try: + self.dap_server.request_disconnect(terminateDebuggee=True) + except (ValueError, TimeoutError, BrokenPipeError, ConnectionError, Exception) as e: + # DAP server might not be responsive, skip disconnect and terminate directly + print(f"Warning: disconnect failed ({e}), skipping and terminating directly") + try: + self.dap_server.terminate() + except Exception as e: + print(f"Warning: terminate failed ({e}), DAP server may have already died") # Execute the cleanup function during test case tear down. self.addTearDownHook(cleanup) @@ -477,9 +484,20 @@ def cleanup(): if expectFailure: return response if not (response and response["success"]): - self.assertTrue( - response["success"], "attach failed (%s)" % (response["message"]) - ) + error_msg = "attach failed" + if response: + if "message" in response: + error_msg += " (%s)" % response["message"] + elif "body" in response and "error" in response["body"]: + if "format" in response["body"]["error"]: + error_msg += " (%s)" % response["body"]["error"]["format"] + else: + error_msg += " (error in body)" + else: + error_msg += " (no error details available)" + else: + error_msg += " (no response)" + self.assertTrue(response and response["success"], error_msg) def launch( self, >From 1c68d3f63f020eaea8aceefc015641fa1fa8e801 Mon Sep 17 00:00:00 2001 From: Piyush Jaiswal <piyushj...@meta.com> Date: Mon, 25 Aug 2025 21:09:59 -0700 Subject: [PATCH 2/3] spliting into 2 PRs and consolidating exeptions --- .../test/tools/lldb-dap/lldbdap_testcase.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py index 8691a87b6fa87..04de3cc6cec0d 100644 --- a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py @@ -468,7 +468,7 @@ def cleanup(): if disconnectAutomatically: try: self.dap_server.request_disconnect(terminateDebuggee=True) - except (ValueError, TimeoutError, BrokenPipeError, ConnectionError, Exception) as e: + except Exception as e: # DAP server might not be responsive, skip disconnect and terminate directly print(f"Warning: disconnect failed ({e}), skipping and terminating directly") try: @@ -484,20 +484,9 @@ def cleanup(): if expectFailure: return response if not (response and response["success"]): - error_msg = "attach failed" - if response: - if "message" in response: - error_msg += " (%s)" % response["message"] - elif "body" in response and "error" in response["body"]: - if "format" in response["body"]["error"]: - error_msg += " (%s)" % response["body"]["error"]["format"] - else: - error_msg += " (error in body)" - else: - error_msg += " (no error details available)" - else: - error_msg += " (no response)" - self.assertTrue(response and response["success"], error_msg) + self.assertTrue( + response["success"], "attach failed (%s)" % (response["message"]) + ) def launch( self, >From 6a2c9a79994856eacc1492e6630dc2595b0ff936 Mon Sep 17 00:00:00 2001 From: Piyush Jaiswal <piyushj...@meta.com> Date: Tue, 26 Aug 2025 11:12:41 -0700 Subject: [PATCH 3/3] Using addTearDownHook and formatting --- .../test/tools/lldb-dap/lldbdap_testcase.py | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py index 04de3cc6cec0d..aef11c42b2cae 100644 --- a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py @@ -450,6 +450,16 @@ def disassemble(self, threadId=None, frameIndex=None): return disassembled_instructions, disassembled_instructions[memoryReference] + def _register_dap_teardown_hooks(self, disconnectAutomatically): + """Register teardown hooks to ensure DAP debug adapter is properly cleaned up. + Uses separate hooks to ensure terminate() is called even if disconnect() fails. + """ + if disconnectAutomatically: + self.addTearDownHook( + lambda: self.dap_server.request_disconnect(terminateDebuggee=True) + ) + self.addTearDownHook(lambda: self.dap_server.terminate()) + def attach( self, *, @@ -464,20 +474,8 @@ def attach( # Make sure we disconnect and terminate the DAP debug adapter even # if we throw an exception during the test case. - def cleanup(): - if disconnectAutomatically: - try: - self.dap_server.request_disconnect(terminateDebuggee=True) - except Exception as e: - # DAP server might not be responsive, skip disconnect and terminate directly - print(f"Warning: disconnect failed ({e}), skipping and terminating directly") - try: - self.dap_server.terminate() - except Exception as e: - print(f"Warning: terminate failed ({e}), DAP server may have already died") - - # Execute the cleanup function during test case tear down. - self.addTearDownHook(cleanup) + self._register_dap_teardown_hooks(disconnectAutomatically) + # Initialize and launch the program self.dap_server.request_initialize(sourceInitFile) response = self.dap_server.request_attach(**kwargs) @@ -500,14 +498,8 @@ def launch( """Sending launch request to dap""" # Make sure we disconnect and terminate the DAP debug adapter, - # if we throw an exception during the test case - def cleanup(): - if disconnectAutomatically: - self.dap_server.request_disconnect(terminateDebuggee=True) - self.dap_server.terminate() - - # Execute the cleanup function during test case tear down. - self.addTearDownHook(cleanup) + # if we throw an exception during the test case. + self._register_dap_teardown_hooks(disconnectAutomatically) # Initialize and launch the program self.dap_server.request_initialize(sourceInitFile) _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits