Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

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

We could add both of these as two distinct cleanups. Exceptions thrown in a addTearDownHook are logged already, but I think the goal of this change is to ensure the .terminate is called. The hooks are called in FIFO order, so we could change this to:

if disconnectAutomatically:
  self.addTearDownHook(lambda: self.dap_server.request_disconnect(terminateDebuggee=True))
self.addTearDownHook(lambda: self.dap_server.terminate())

Copy link
Contributor Author

@piyushjaiswal98 piyushjaiswal98 Aug 26, 2025

Choose a reason for hiding this comment

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

Thanks for the suggestion. I think adding both as separate tear down hooks makes sense. I'll update the PR

Copy link
Contributor Author

@piyushjaiswal98 piyushjaiswal98 Aug 28, 2025

Choose a reason for hiding this comment

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

On investigation...addTearDownHook appears to follow a LIFO order...This approach doesn't seem to work... falling back to the original

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)
Expand All @@ -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,
Expand Down