Skip to content

Commit f45bba5

Browse files
committed
Moving cancel checking into RequestHandler to try to conslidate the cancellation logic.
1 parent 8a1c97f commit f45bba5

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <future>
4848
#include <memory>
4949
#include <mutex>
50+
#include <optional>
5051
#include <string>
5152
#include <utility>
5253

@@ -70,15 +71,6 @@ const char DEV_NULL[] = "/dev/null";
7071
#endif
7172
} // namespace
7273

73-
static Response CancelledResponse(int64_t seq, std::string command) {
74-
Response response;
75-
response.request_seq = seq;
76-
response.command = command;
77-
response.success = false;
78-
response.message = Response::Message::cancelled;
79-
return response;
80-
}
81-
8274
namespace lldb_dap {
8375

8476
DAP::DAP(llvm::StringRef path, Log *log, const ReplMode default_repl_mode,
@@ -253,11 +245,17 @@ void DAP::SendJSON(const llvm::json::Value &json) {
253245
}
254246

255247
void DAP::Send(const Message &message) {
248+
// FIXME: After all the requests have migrated from LegacyRequestHandler >
249+
// RequestHandler<> this should be handled in RequestHandler<>::operator().
256250
if (auto *resp = std::get_if<Response>(&message);
257251
resp && debugger.InterruptRequested()) {
258252
// If the debugger was interrupted, convert this response into a 'cancelled'
259253
// response because we might have a partial result.
260-
Response cancelled = CancelledResponse(resp->request_seq, resp->command);
254+
Response cancelled{/*request_seq=*/resp->request_seq,
255+
/*command=*/resp->command,
256+
/*success=*/false,
257+
/*message=*/Response::Message::cancelled,
258+
/*body=*/std::nullopt};
261259
if (llvm::Error err = transport.Write(cancelled))
262260
DAP_LOG_ERROR(log, std::move(err), "({1}) write failed: {0}",
263261
transport.GetClientName());
@@ -711,20 +709,9 @@ bool DAP::HandleObject(const Message &M) {
711709
m_active_request = nullptr;
712710
});
713711

714-
{
715-
// If there is a pending cancelled request, preempt the request and mark
716-
// it cancelled.
717-
std::lock_guard<std::mutex> lock(m_cancelled_requests_mutex);
718-
if (m_cancelled_requests.find(req->seq) != m_cancelled_requests.end()) {
719-
Response cancelled = CancelledResponse(req->seq, req->command);
720-
Send(cancelled);
721-
return true;
722-
}
723-
}
724-
725712
auto handler_pos = request_handlers.find(req->command);
726713
if (handler_pos != request_handlers.end()) {
727-
(*handler_pos->second)(*req);
714+
handler_pos->second->Run(*req);
728715
return true; // Success
729716
}
730717

@@ -825,6 +812,11 @@ llvm::Error DAP::Disconnect(bool terminateDebuggee) {
825812
return ToError(error);
826813
}
827814

815+
bool DAP::IsCancelled(const protocol::Request &req) {
816+
std::lock_guard<std::mutex> lock(m_cancelled_requests_mutex);
817+
return m_cancelled_requests.find(req.seq) != m_cancelled_requests.end();
818+
}
819+
828820
void DAP::ClearCancelRequest(const CancelArguments &args) {
829821
std::lock_guard<std::mutex> cancalled_requests_lock(
830822
m_cancelled_requests_mutex);

lldb/tools/lldb-dap/DAP.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ struct DAP {
403403

404404
InstructionBreakpoint *GetInstructionBPFromStopReason(lldb::SBThread &thread);
405405

406+
/// Checks if the request is cancelled.
407+
bool IsCancelled(const protocol::Request &);
408+
406409
/// Clears the cancel request from the set of tracked cancel requests.
407410
void ClearCancelRequest(const protocol::CancelArguments &);
408411

lldb/tools/lldb-dap/Handler/RequestHandler.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <unistd.h>
1818
#endif
1919

20+
using namespace lldb_dap::protocol;
21+
2022
namespace lldb_dap {
2123

2224
static std::vector<const char *>
@@ -159,6 +161,28 @@ static llvm::Error RunInTerminal(DAP &dap,
159161
error.GetCString());
160162
}
161163

164+
void BaseRequestHandler::Run(const Request &request) {
165+
// If this request was cancelled, send a cancelled response.
166+
if (dap.IsCancelled(request)) {
167+
Response cancelled{/*request_seq=*/request.seq,
168+
/*command=*/request.command,
169+
/*success=*/false,
170+
/*message=*/Response::Message::cancelled,
171+
/*body=*/std::nullopt};
172+
dap.Send(cancelled);
173+
return;
174+
}
175+
176+
// FIXME: After all the requests have migrated from LegacyRequestHandler >
177+
// RequestHandler<> we should be able to move this into
178+
// RequestHandler<>::operator().
179+
operator()(request);
180+
181+
// FIXME: After all the requests have migrated from LegacyRequestHandler >
182+
// RequestHandler<> we should be able to check `debugger.InterruptRequest` and
183+
// mark the response as cancelled.
184+
}
185+
162186
lldb::SBError
163187
BaseRequestHandler::LaunchProcess(const llvm::json::Object &request) const {
164188
lldb::SBError error;

lldb/tools/lldb-dap/Handler/RequestHandler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class BaseRequestHandler {
4343

4444
virtual ~BaseRequestHandler() = default;
4545

46+
void Run(const protocol::Request &);
47+
4648
virtual void operator()(const protocol::Request &request) const = 0;
4749

4850
virtual llvm::StringMap<bool> GetCapabilities() const { return {}; }

0 commit comments

Comments
 (0)