Skip to content

Commit 720e59f

Browse files
committed
Applying reviewer suggestions and have the reader loop break when the "disconnect" request is detected.
1 parent a56e05a commit 720e59f

File tree

1 file changed

+61
-63
lines changed

1 file changed

+61
-63
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <optional>
5252
#include <string>
5353
#include <utility>
54+
#include <variant>
5455

5556
#if defined(_WIN32)
5657
#define NOMINMAX
@@ -700,7 +701,7 @@ void DAP::SetTarget(const lldb::SBTarget target) {
700701
bool DAP::HandleObject(const Message &M) {
701702
if (const auto *req = std::get_if<Request>(&M)) {
702703
{
703-
std::lock_guard<std::mutex> lock(m_active_request_mutex);
704+
std::lock_guard<std::mutex> guard(m_active_request_mutex);
704705
m_active_request = req;
705706

706707
// Clear the interrupt request prior to invoking a handler.
@@ -727,7 +728,7 @@ bool DAP::HandleObject(const Message &M) {
727728
if (const auto *resp = std::get_if<Response>(&M)) {
728729
std::unique_ptr<ResponseHandler> response_handler;
729730
{
730-
std::lock_guard<std::mutex> locker(call_mutex);
731+
std::lock_guard<std::mutex> guard(call_mutex);
731732
auto inflight = inflight_reverse_requests.find(resp->request_seq);
732733
if (inflight != inflight_reverse_requests.end()) {
733734
response_handler = std::move(inflight->second);
@@ -818,13 +819,12 @@ llvm::Error DAP::Disconnect(bool terminateDebuggee) {
818819
}
819820

820821
bool DAP::IsCancelled(const protocol::Request &req) {
821-
std::lock_guard<std::mutex> lock(m_cancelled_requests_mutex);
822+
std::lock_guard<std::mutex> guard(m_cancelled_requests_mutex);
822823
return m_cancelled_requests.contains(req.seq);
823824
}
824825

825826
void DAP::ClearCancelRequest(const CancelArguments &args) {
826-
std::lock_guard<std::mutex> cancalled_requests_lock(
827-
m_cancelled_requests_mutex);
827+
std::lock_guard<std::mutex> guard(m_cancelled_requests_mutex);
828828
if (args.requestId)
829829
m_cancelled_requests.erase(*args.requestId);
830830
}
@@ -846,69 +846,67 @@ static std::optional<T> getArgumentsIfRequest(const Message &pm,
846846
}
847847

848848
llvm::Error DAP::Loop() {
849-
std::future<llvm::Error> queue_reader = std::async([&]() -> llvm::Error {
850-
llvm::set_thread_name(transport.GetClientName() + ".transport_handler");
851-
auto cleanup = llvm::make_scope_exit([&]() {
852-
// Ensure we're marked as disconnecting when the reader exits.
853-
disconnecting = true;
854-
m_queue_cv.notify_all();
855-
});
856-
857-
while (!disconnecting) {
858-
llvm::Expected<Message> next = transport.Read(std::chrono::seconds(1));
859-
bool timeout = false;
860-
bool eof = false;
861-
if (llvm::Error Err = llvm::handleErrors(
862-
next.takeError(),
863-
[&](const EndOfFileError &E) -> llvm::Error {
864-
eof = true;
865-
return llvm::Error::success();
866-
},
867-
[&](const TimeoutError &) -> llvm::Error {
868-
timeout = true;
869-
return llvm::Error::success();
870-
}))
871-
return Err;
872-
873-
if (eof)
874-
break;
875-
876-
// If the read timed out, continue to check if we should disconnect.
877-
if (timeout)
878-
continue;
879-
880-
const std::optional<CancelArguments> cancel_args =
881-
getArgumentsIfRequest<CancelArguments>(*next, "cancel");
882-
if (cancel_args) {
883-
{
884-
std::lock_guard<std::mutex> cancalled_requests_lock(
885-
m_cancelled_requests_mutex);
886-
if (cancel_args->requestId)
887-
m_cancelled_requests.insert(*cancel_args->requestId);
888-
}
889-
890-
// If a cancel is requested for the active request, make a best
891-
// effort attempt to interrupt.
892-
std::lock_guard<std::mutex> active_request_lock(m_active_request_mutex);
893-
if (m_active_request &&
894-
cancel_args->requestId == m_active_request->seq) {
895-
DAP_LOG(log,
849+
std::future<llvm::Error> queue_reader =
850+
std::async(std::launch::async, [&]() -> llvm::Error {
851+
llvm::set_thread_name(transport.GetClientName() + ".transport_handler");
852+
auto cleanup = llvm::make_scope_exit([&]() {
853+
// Ensure we're marked as disconnecting when the reader exits.
854+
disconnecting = true;
855+
m_queue_cv.notify_all();
856+
});
857+
858+
while (!disconnecting) {
859+
llvm::Expected<Message> next =
860+
transport.Read(std::chrono::seconds(1));
861+
if (next.errorIsA<EndOfFileError>()) {
862+
consumeError(next.takeError());
863+
break;
864+
}
865+
866+
// If the read timed out, continue to check if we should disconnect.
867+
if (next.errorIsA<TimeoutError>()) {
868+
consumeError(next.takeError());
869+
continue;
870+
}
871+
872+
if (const protocol::Request *req =
873+
std::get_if<protocol::Request>(&*next);
874+
req && req->command == "disconnect") {
875+
disconnecting = true;
876+
}
877+
878+
const std::optional<CancelArguments> cancel_args =
879+
getArgumentsIfRequest<CancelArguments>(*next, "cancel");
880+
if (cancel_args) {
881+
{
882+
std::lock_guard<std::mutex> guard(m_cancelled_requests_mutex);
883+
if (cancel_args->requestId)
884+
m_cancelled_requests.insert(*cancel_args->requestId);
885+
}
886+
887+
// If a cancel is requested for the active request, make a best
888+
// effort attempt to interrupt.
889+
std::lock_guard<std::mutex> guard(m_active_request_mutex);
890+
if (m_active_request &&
891+
cancel_args->requestId == m_active_request->seq) {
892+
DAP_LOG(
893+
log,
896894
"({0}) interrupting inflight request (command={1} seq={2})",
897895
transport.GetClientName(), m_active_request->command,
898896
m_active_request->seq);
899-
debugger.RequestInterrupt();
897+
debugger.RequestInterrupt();
898+
}
899+
}
900+
901+
{
902+
std::lock_guard<std::mutex> guard(m_queue_mutex);
903+
m_queue.push_back(std::move(*next));
904+
}
905+
m_queue_cv.notify_one();
900906
}
901-
}
902907

903-
{
904-
std::lock_guard<std::mutex> queue_lock(m_queue_mutex);
905-
m_queue.push_back(std::move(*next));
906-
}
907-
m_queue_cv.notify_one();
908-
}
909-
910-
return llvm::Error::success();
911-
});
908+
return llvm::Error::success();
909+
});
912910

913911
auto cleanup = llvm::make_scope_exit([&]() {
914912
out.Stop();

0 commit comments

Comments
 (0)