Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,20 @@ def disassemble(self, threadId=None, frameIndex=None):

return disassembled_instructions, disassembled_instructions[memoryReference]

def dapCleanup(self, disconnectAutomatically):
if disconnectAutomatically:
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is swallowing any exceptions that can occur in either request_disconnect or terminate, which is different behavior than the version originally approved by @walter-erquinigo and @ashgti.

@piyushjaiswal98 says that the teardown hooks are actually processed in LIFO order instead of FIFO order as expected which is why he changed it to catching them. Is there a better way to handle the exceptions here?

Can we use try-finally instead?

def dapCleanup(self, disconnectAutomatically):
    try:
        if disconnectAutomatically:
            self.dap_server.request_disconnect(terminateDebuggee=True)
    finally:
        self.dap_server.terminate()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a good idea

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")

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 +497,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.addTearDownHook(lambda: self.dapCleanup(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 +520,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.addTearDownHook(lambda: self.dapCleanup(disconnectAutomatically))

# Initialize and launch the program
self.dap_server.request_initialize(sourceInitFile)
Expand Down
Loading