Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -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 _build_error_message(self, base_message, response):
"""Build a detailed error message from a DAP response.
Extracts error information from various possible locations in the response structure.
Expand Down Expand Up @@ -483,13 +493,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:
self.dap_server.request_disconnect(terminateDebuggee=True)
self.dap_server.terminate()
self._register_dap_teardown_hooks(disconnectAutomatically)

# Execute the cleanup function during test case tear down.
self.addTearDownHook(cleanup)
# Initialize and launch the program
self.dap_server.request_initialize(sourceInitFile)
response = self.dap_server.request_attach(**kwargs)
Expand All @@ -511,14 +516,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)
Expand Down
Loading