Skip to content

[lldb-dap] Adding support for cancelling a request. #130169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lldb/test/API/tools/lldb-dap/cancel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
C_SOURCES := main.c

include Makefile.rules
101 changes: 101 additions & 0 deletions lldb/test/API/tools/lldb-dap/cancel/TestDAP_cancel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""
Test lldb-dap cancel request
"""

import time

from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
import lldbdap_testcase


class TestDAP_launch(lldbdap_testcase.DAPTestCaseBase):
def send_async_req(self, command: str, arguments={}) -> int:
seq = self.dap_server.sequence
self.dap_server.send_packet(
{
"type": "request",
"command": command,
"arguments": arguments,
}
)
return seq

def async_blocking_request(self, duration: float) -> int:
"""
Sends an evaluate request that will sleep for the specified duration to
block the request handling thread.
"""
return self.send_async_req(
command="evaluate",
arguments={
"expression": '`script import time; print("starting sleep", file=lldb.debugger.GetOutputFileHandle()); time.sleep({})'.format(
duration
),
"context": "repl",
},
)

def async_cancel(self, requestId: int) -> int:
return self.send_async_req(command="cancel", arguments={"requestId": requestId})

def test_pending_request(self):
"""
Tests cancelling a pending request.
"""
program = self.getBuildArtifact("a.out")
self.build_and_launch(program, stopOnEntry=True)
self.continue_to_next_stop()

# Use a relatively short timeout since this is only to ensure the
# following request is queued.
blocking_seq = self.async_blocking_request(duration=1.0)
# Use a longer timeout to ensure we catch if the request was interrupted
# properly.
pending_seq = self.async_blocking_request(duration=self.timeoutval / 2)
cancel_seq = self.async_cancel(requestId=pending_seq)

blocking_resp = self.dap_server.recv_packet(filter_type=["response"])
self.assertEqual(blocking_resp["request_seq"], blocking_seq)
self.assertEqual(blocking_resp["command"], "evaluate")
self.assertEqual(blocking_resp["success"], True)

pending_resp = self.dap_server.recv_packet(filter_type=["response"])
self.assertEqual(pending_resp["request_seq"], pending_seq)
self.assertEqual(pending_resp["command"], "evaluate")
self.assertEqual(pending_resp["success"], False)
self.assertEqual(pending_resp["message"], "cancelled")

cancel_resp = self.dap_server.recv_packet(filter_type=["response"])
self.assertEqual(cancel_resp["request_seq"], cancel_seq)
self.assertEqual(cancel_resp["command"], "cancel")
self.assertEqual(cancel_resp["success"], True)
self.continue_to_exit()

def test_inflight_request(self):
"""
Tests cancelling an inflight request.
"""
program = self.getBuildArtifact("a.out")
self.build_and_launch(program, stopOnEntry=True)
self.continue_to_next_stop()

blocking_seq = self.async_blocking_request(duration=self.timeoutval / 2)
# Wait for the sleep to start to cancel the inflight request.
self.collect_stdout(
timeout_secs=self.timeoutval,
pattern="starting sleep",
)
cancel_seq = self.async_cancel(requestId=blocking_seq)

blocking_resp = self.dap_server.recv_packet(filter_type=["response"])
self.assertEqual(blocking_resp["request_seq"], blocking_seq)
self.assertEqual(blocking_resp["command"], "evaluate")
self.assertEqual(blocking_resp["success"], False)
self.assertEqual(blocking_resp["message"], "cancelled")

cancel_resp = self.dap_server.recv_packet(filter_type=["response"])
self.assertEqual(cancel_resp["request_seq"], cancel_seq)
self.assertEqual(cancel_resp["command"], "cancel")
self.assertEqual(cancel_resp["success"], True)
self.continue_to_exit()
6 changes: 6 additions & 0 deletions lldb/test/API/tools/lldb-dap/cancel/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main(int argc, char const *argv[]) {
printf("Hello world!\n");
return 0;
}
1 change: 1 addition & 0 deletions lldb/test/API/tools/lldb-dap/launch/TestDAP_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_default(self):
lines = output.splitlines()
self.assertIn(program, lines[0], "make sure program path is in first argument")

@skipIfWindows
Copy link
Member

@vogelsgesang vogelsgesang Mar 19, 2025

Choose a reason for hiding this comment

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

why? Did this test case work previously and was now broken by implementing cancellation support?

def test_termination(self):
"""
Tests the correct termination of lldb-dap upon a 'disconnect'
Expand Down
1 change: 1 addition & 0 deletions lldb/tools/lldb-dap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ add_lldb_tool(lldb-dap
Handler/ResponseHandler.cpp
Handler/AttachRequestHandler.cpp
Handler/BreakpointLocationsHandler.cpp
Handler/CancelRequestHandler.cpp
Handler/CompileUnitsRequestHandler.cpp
Handler/CompletionsHandler.cpp
Handler/ConfigurationDoneRequestHandler.cpp
Expand Down
Loading
Loading