Debian Codegraph @

LspCpp/src/jsonrpc/RemoteEndPoint.cpp

raw
1 #include "LibLsp/JsonRpc/MessageJsonHandler.h"
2 #include "LibLsp/JsonRpc/Endpoint.h"
3 #include "LibLsp/JsonRpc/message.h"
4 #include "LibLsp/JsonRpc/RemoteEndPoint.h"
5 #include <future>
6 #include "LibLsp/JsonRpc/Cancellation.h"
7 #include "LibLsp/JsonRpc/StreamMessageProducer.h"
8 #include "LibLsp/JsonRpc/NotificationInMessage.h"
9 #include "LibLsp/JsonRpc/lsResponseMessage.h"
10 #include "LibLsp/JsonRpc/Condition.h"
11 #include "LibLsp/JsonRpc/Context.h"
12 #include "rapidjson/error/en.h"
13 #include "LibLsp/JsonRpc/json.h"
14 #include "LibLsp/JsonRpc/ScopeExit.h"
15 #include "LibLsp/JsonRpc/stream.h"
16 #include <atomic>
17 #include <optional>
18 #ifdef LSPCPP_USE_STANDALONE_ASIO
19 #include <asio/thread_pool.hpp>
20 #include <asio/post.hpp>
21 #else
22 #include <boost/asio/thread_pool.hpp>
23 #include <boost/asio/post.hpp>
24 namespace asio = boost::asio;
25 #endif
26
27 #include "LibLsp/JsonRpc/GCThreadContext.h"
28
29 namespace lsp
30 {
31
32 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
33 // See https://llvm.org/LICENSE.txt for license information.
34 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
35 //===----------------------------------------------------------------------===//
36
37 // Cancellation mechanism for long-running tasks.
38 //
39 // This manages interactions between:
40 //
41 // 1. Client code that starts some long-running work, and maybe cancels later.
42 //
43 // std::pair<Context, Canceler> Task = cancelableTask();
44 // {
45 // WithContext Cancelable(std::move(Task.first));
46 // Expected
47 // deepThoughtAsync([](int answer){ errs() << answer; });
48 // }
49 // // ...some time later...
50 // if (User.fellAsleep())
51 // Task.second();
52 //
53 // (This example has an asynchronous computation, but synchronous examples
54 // work similarly - the Canceler should be invoked from another thread).
55 //
56 // 2. Library code that executes long-running work, and can exit early if the
57 // result is not needed.
58 //
59 // void deepThoughtAsync(std::function<void(int)> Callback) {
60 // runAsync([Callback]{
61 // int A = ponder(6);
62 // if (getCancelledMonitor())
63 // return;
64 // int B = ponder(9);
65 // if (getCancelledMonitor())
66 // return;
67 // Callback(A * B);
68 // });
69 // }
70 //
71 // (A real example may invoke the callback with an error on cancellation,
72 // the CancelledError is provided for this purpose).
73 //
74 // Cancellation has some caveats:
75 // - the work will only stop when/if the library code next checks for it.
76 // Code outside clangd such as Sema will not do this.
77 // - it's inherently racy: client code must be prepared to accept results
78 // even after requesting cancellation.
79 // - it's Context-based, so async work must be dispatched to threads in
80 // ways that preserve the context. (Like runAsync() or TUScheduler).
81 //
82
83 /// A canceller requests cancellation of a task, when called.
84 /// Calling it again has no effect.
85 using Canceler = std::function<void()>;
86
87 // We don't want a cancelable scope to "shadow" an enclosing one.
88 struct CancelState
89 {
90 std::shared_ptr<std::atomic<int>> cancelled;
91 CancelState const* parent = nullptr;
92 lsRequestId id;
93 };
94 static Key<CancelState> g_stateKey;
95
96 /// Defines a new task whose cancellation may be requested.
97 /// The returned Context defines the scope of the task.
98 /// When the context is active, getCancelledMonitor() is 0 until the Canceler is
99 /// invoked, and equal to Reason afterwards.
100 /// Conventionally, Reason may be the LSP error code to return.
101 std::pair<Context, Canceler> cancelableTask(lsRequestId const& id, int reason = 1)
102 {
103 assert(reason != 0 && "Can't detect cancellation if Reason is zero");
104 CancelState state;
105 state.id = id;
106 state.cancelled = std::make_shared<std::atomic<int>>();
107 state.parent = Context::current().get(g_stateKey);
108 return {
109 Context::current().derive(g_stateKey, state),
110 [reason, cancelled(state.cancelled)] { *cancelled = reason; },
111 };
112 }
113 /// If the current context is within a cancelled task, returns the reason.
114 /// (If the context is within multiple nested tasks, true if any are cancelled).
115 /// Always zero if there is no active cancelable task.
116 /// This isn't free (context lookup) - don't call it in a tight loop.
117 optional<CancelMonitor> getCancelledMonitor(lsRequestId const& id, Context const& ctx = Context::current())
118 {
119 for (CancelState const* state = ctx.get(g_stateKey); state != nullptr; state = state->parent)
120 {
121 if (id != state->id)
122 {
123 continue;
124 }
125 std::shared_ptr<std::atomic<int>> const cancelled = state->cancelled;
126 std::function<int()> temp = [=] { return cancelled->load(); };
127 return std::move(temp);
128 }
129
130 return {};
131 }
132 } // namespace lsp
133
134 using namespace lsp;
135 class PendingRequestInfo
136 {
137 using RequestCallBack = std::function<bool(std::unique_ptr<LspMessage>)>;
138
139 public:
140 PendingRequestInfo(std::string const& md, RequestCallBack const& callback);
141 PendingRequestInfo(std::string const& md);
142 PendingRequestInfo()
143 {
144 }
145 std::string method;
146 RequestCallBack futureInfo;
147 };
148
149 PendingRequestInfo::PendingRequestInfo(std::string const& _md, RequestCallBack const& callback)
150 : method(_md), futureInfo(callback)
151 {
152 }
153
154 PendingRequestInfo::PendingRequestInfo(std::string const& md) : method(md)
155 {
156 }
157 struct RemoteEndPoint::Data
158 {
159 explicit Data(lsp::JSONStreamStyle style, uint8_t workers, lsp::Log& _log, RemoteEndPoint* owner)
160 : max_workers(workers), m_id(0), next_request_cookie(0), log(_log)
161 {
162 if (style == lsp::JSONStreamStyle::Standard)
163 {
164 message_producer = (new LSPStreamMessageProducer(*owner));
165 }
166 else
167 {
168 message_producer = (new DelimitedStreamMessageProducer(*owner));
169 }
170 }
171 ~Data()
172 {
173 delete message_producer;
174 }
175 uint8_t max_workers;
176 std::atomic<int> m_id;
177 std::shared_ptr<asio::thread_pool> tp;
178 // Method calls may be cancelled by ID, so keep track of their state.
179 // This needs a mutex: handlers may finish on a different thread, and that's
180 // when we clean up entries in the map.
181 mutable std::mutex request_cancelers_mutex;
182
183 std::map<lsRequestId, std::pair<Canceler, /*Cookie*/ unsigned>> requestCancelers;
184
185 std::atomic<unsigned> next_request_cookie; // To disambiguate reused IDs, see below.
186 void onCancel(Notify_Cancellation::notify* notify)
187 {
188 std::lock_guard<std::mutex> Lock(request_cancelers_mutex);
189 auto const it = requestCancelers.find(notify->params.id);
190 if (it != requestCancelers.end())
191 {
192 it->second.first(); // Invoke the canceler.
193 }
194 }
195
196 // We run cancelable requests in a context that does two things:
197 // - allows cancellation using requestCancelers[ID]
198 // - cleans up the entry in requestCancelers when it's no longer needed
199 // If a client reuses an ID, the last wins and the first cannot be canceled.
200 Context cancelableRequestContext(lsRequestId id)
201 {
202 auto task = cancelableTask(
203 id,
204 /*Reason=*/static_cast<int>(lsErrorCodes::RequestCancelled)
205 );
206 unsigned cookie;
207 {
208 std::lock_guard<std::mutex> Lock(request_cancelers_mutex);
209 cookie = next_request_cookie.fetch_add(1, std::memory_order_relaxed);
210 requestCancelers[id] = {std::move(task.second), cookie};
211 }
212 // When the request ends, we can clean up the entry we just added.
213 // The cookie lets us check that it hasn't been overwritten due to ID
214 // reuse.
215 return task.first.derive(lsp::make_scope_exit(
216 [this, id, cookie]
217 {
218 std::lock_guard<std::mutex> lock(request_cancelers_mutex);
219 auto const& it = requestCancelers.find(id);
220 if (it != requestCancelers.end() && it->second.second == cookie)
221 {
222 requestCancelers.erase(it);
223 }
224 }
225 ));
226 }
227
228 std::map<lsRequestId, std::shared_ptr<PendingRequestInfo>> _client_request_futures;
229 StreamMessageProducer* message_producer;
230 std::atomic<bool> quit {};
231 lsp::Log& log;
232 std::shared_ptr<lsp::istream> input;
233 std::shared_ptr<lsp::ostream> output;
234
235 std::mutex m_requestInfo;
236
237 bool pendingRequest(RequestInMessage& info, GenericResponseHandler&& handler)
238 {
239 bool ret = true;
240 std::lock_guard<std::mutex> lock(m_requestInfo);
241 if (!info.id.has_value())
242 {
243 auto id = getNextRequestId();
244 info.id.set(id);
245 }
246 else
247 {
248 if (_client_request_futures.find(info.id) != _client_request_futures.end())
249 {
250 ret = false;
251 }
252 }
253 _client_request_futures[info.id] = std::make_shared<PendingRequestInfo>(info.method, handler);
254 return ret;
255 }
256 std::shared_ptr<PendingRequestInfo const> const getRequestInfo(lsRequestId const& _id)
257 {
258 std::lock_guard<std::mutex> lock(m_requestInfo);
259 auto findIt = _client_request_futures.find(_id);
260 if (findIt != _client_request_futures.end())
261 {
262 return findIt->second;
263 }
264 return nullptr;
265 }
266
267 void removeRequestInfo(lsRequestId const& _id)
268 {
269 std::lock_guard<std::mutex> lock(m_requestInfo);
270 auto findIt = _client_request_futures.find(_id);
271 if (findIt != _client_request_futures.end())
272 {
273 _client_request_futures.erase(findIt);
274 }
275 }
276 void clear()
277 {
278 {
279 std::lock_guard<std::mutex> lock(m_requestInfo);
280 _client_request_futures.clear();
281 }
282 if (tp)
283 {
284 tp->stop();
285 }
286 quit.store(true, std::memory_order_relaxed);
287 }
288
289 int getNextRequestId()
290 {
291 return m_id.fetch_add(1, std::memory_order_relaxed);
292 }
293 };
294
295 namespace
296 {
297 void WriterMsg(std::shared_ptr<lsp::ostream>& output, LspMessage& msg)
298 {
299 auto const& s = msg.ToJson();
300 auto const value = std::string("Content-Length: ") + std::to_string(s.size()) + "\r\n\r\n" + s;
301 output->write(value);
302 output->flush();
303 }
304
305 bool isResponseMessage(JsonReader& visitor)
306 {
307
308 if (!visitor.HasMember("id"))
309 {
310 return false;
311 }
312
313 if (!visitor.HasMember("result") && !visitor.HasMember("error"))
314 {
315 return false;
316 }
317
318 return true;
319 }
320
321 bool isRequestMessage(JsonReader& visitor)
322 {
323 if (!visitor.HasMember("method"))
324 {
325 return false;
326 }
327 if (!visitor["method"]->IsString())
328 {
329 return false;
330 }
331 if (!visitor.HasMember("id"))
332 {
333 return false;
334 }
335 return true;
336 }
337 bool isNotificationMessage(JsonReader& visitor)
338 {
339 if (!visitor.HasMember("method"))
340 {
341 return false;
342 }
343 if (!visitor["method"]->IsString())
344 {
345 return false;
346 }
347 if (visitor.HasMember("id"))
348 {
349 return false;
350 }
351 return true;
352 }
353 } // namespace
354
355 CancelMonitor RemoteEndPoint::getCancelMonitor(lsRequestId const& id)
356 {
357 auto monitor = getCancelledMonitor(id);
358 if (monitor.has_value())
359 {
360 return monitor.value();
361 }
362 return [] { return 0; };
363 }
364
365 RemoteEndPoint::RemoteEndPoint(
366 std::shared_ptr<MessageJsonHandler> const& json_handler, std::shared_ptr<Endpoint> const& localEndPoint,
367 lsp::Log& _log, lsp::JSONStreamStyle style, uint8_t max_workers
368 )
369 : d_ptr(new Data(style, max_workers, _log, this)), jsonHandler(json_handler), local_endpoint(localEndPoint)
370 {
371 jsonHandler->method2notification[Notify_Cancellation::notify::kMethodInfo] = [](Reader& visitor)
372 { return Notify_Cancellation::notify::ReflectReader(visitor); };
373
374 d_ptr->quit.store(false, std::memory_order_relaxed);
375 }
376
377 RemoteEndPoint::~RemoteEndPoint()
378 {
379 d_ptr->quit.store(true, std::memory_order_relaxed);
380 // if (this->message_producer_thread_ && this->message_producer_thread_->joinable())
381 // {
382 // this->message_producer_thread_->join();
383 // }
384 delete d_ptr;
385 d_ptr = nullptr;
386 }
387
388 bool RemoteEndPoint::dispatch(std::string const& content)
389 {
390 rapidjson::Document document;
391 document.Parse(content.c_str(), content.length());
392 if (document.HasParseError())
393 {
394 std::string info = "lsp msg format error:";
395 rapidjson::GetParseErrorFunc GetParseError = rapidjson::GetParseError_En; // or whatever
396 info += GetParseError(document.GetParseError());
397 info += "\n";
398 info += "ErrorContext offset:\n";
399 info += content.substr(document.GetErrorOffset());
400 d_ptr->log.log(Log::Level::SEVERE, info);
401
402 return false;
403 }
404
405 JsonReader visitor {&document};
406 if (!visitor.HasMember("jsonrpc") || std::string(visitor["jsonrpc"]->GetString()) != "2.0")
407 {
408 std::string reason;
409 reason = "Reason:Bad or missing jsonrpc version\n";
410 reason += "content:\n" + content;
411 d_ptr->log.log(Log::Level::SEVERE, reason);
412 return false;
413 }
414 LspMessage::Kind _kind = LspMessage::NOTIFICATION_MESSAGE;
415 try
416 {
417 if (isRequestMessage(visitor))
418 {
419 _kind = LspMessage::REQUEST_MESSAGE;
420 auto msg = jsonHandler->parseRequstMessage(visitor["method"]->GetString(), visitor);
421 if (msg)
422 {
423 mainLoop(std::move(msg));
424 }
425 else
426 {
427 std::string info = "Unknown support request message when consumer message:\n";
428 info += content;
429 d_ptr->log.log(Log::Level::WARNING, info);
430 return false;
431 }
432 }
433 else if (isResponseMessage(visitor))
434 {
435 _kind = LspMessage::RESPONCE_MESSAGE;
436 lsRequestId id;
437 ReflectMember(visitor, "id", id);
438
439 auto msgInfo = d_ptr->getRequestInfo(id);
440 if (!msgInfo)
441 {
442 std::string info = "Unknown response message :\n";
443 info += content;
444 d_ptr->log.log(Log::Level::INFO, info);
445 }
446 else
447 {
448
449 auto msg = jsonHandler->parseResponseMessage(msgInfo->method, visitor);
450 if (msg)
451 {
452 mainLoop(std::move(msg));
453 }
454 else
455 {
456 std::string info = "Unknown response message :\n";
457 info += content;
458 d_ptr->log.log(Log::Level::SEVERE, info);
459 return false;
460 }
461 }
462 }
463 else if (isNotificationMessage(visitor))
464 {
465 auto msg = jsonHandler->parseNotificationMessage(visitor["method"]->GetString(), visitor);
466 if (!msg)
467 {
468 std::string info = "Unknown notification message :\n";
469 info += content;
470 d_ptr->log.log(Log::Level::SEVERE, info);
471 return false;
472 }
473 mainLoop(std::move(msg));
474 }
475 else
476 {
477 std::string info = "Unknown lsp message when consumer message:\n";
478 info += content;
479 d_ptr->log.log(Log::Level::WARNING, info);
480 return false;
481 }
482 }
483 catch (std::exception& e)
484 {
485
486 std::string info = "Exception when process ";
487 if (_kind == LspMessage::REQUEST_MESSAGE)
488 {
489 info += "request";
490 }
491 if (_kind == LspMessage::RESPONCE_MESSAGE)
492 {
493 info += "response";
494 }
495 else
496 {
497 info += "notification";
498 }
499 info += " message:\n";
500 info += e.what();
501 std::string reason = "Reason:" + info + "\n";
502 reason += "content:\n" + content;
503 d_ptr->log.log(Log::Level::SEVERE, reason);
504 return false;
505 }
506 return true;
507 }
508
509 bool RemoteEndPoint::internalSendRequest(RequestInMessage& info, GenericResponseHandler handler)
510 {
511 std::lock_guard<std::mutex> lock(m_sendMutex);
512 if (!d_ptr->output || d_ptr->output->bad())
513 {
514 std::string desc = "Output isn't good any more:\n";
515 d_ptr->log.log(Log::Level::WARNING, desc);
516 return false;
517 }
518 if (!d_ptr->pendingRequest(info, std::move(handler)))
519 {
520 std::string desc = "Duplicate id which of request:";
521 desc += info.ToJson();
522 desc += "\n";
523 d_ptr->log.log(Log::Level::WARNING, desc);
524 }
525 WriterMsg(d_ptr->output, info);
526 return true;
527 }
528
529 int RemoteEndPoint::getNextRequestId()
530 {
531 return d_ptr->getNextRequestId();
532 }
533 bool RemoteEndPoint::cancelRequest(lsRequestId const& id)
534 {
535 if (!isWorking())
536 {
537 return false;
538 }
539 auto msgInfo = d_ptr->getRequestInfo(id);
540 if (msgInfo)
541 {
542 Notify_Cancellation::notify cancel_notify;
543 cancel_notify.params.id = id;
544 send(cancel_notify);
545 return true;
546 }
547 return false;
548 }
549 std::unique_ptr<LspMessage> RemoteEndPoint::internalWaitResponse(RequestInMessage& request, unsigned time_out)
550 {
551 auto eventFuture = std::make_shared<Condition<LspMessage>>();
552 internalSendRequest(
553 request,
554 [=](std::unique_ptr<LspMessage> data)
555 {
556 eventFuture->notify(std::move(data));
557 return true;
558 }
559 );
560 return eventFuture->wait(time_out);
561 }
562
563 void RemoteEndPoint::mainLoop(std::unique_ptr<LspMessage> msg)
564 {
565 if (d_ptr->quit.load(std::memory_order_relaxed))
566 {
567 return;
568 }
569 auto const _kind = msg->GetKid();
570 if (_kind == LspMessage::REQUEST_MESSAGE)
571 {
572 auto req = static_cast<RequestInMessage*>(msg.get());
573 // Calls can be canceled by the client. Add cancellation context.
574 WithContext WithCancel(d_ptr->cancelableRequestContext(req->id));
575 local_endpoint->onRequest(std::move(msg));
576 }
577
578 else if (_kind == LspMessage::RESPONCE_MESSAGE)
579 {
580 auto const id = static_cast<ResponseInMessage*>(msg.get())->id;
581 auto msgInfo = d_ptr->getRequestInfo(id);
582 if (!msgInfo)
583 {
584 auto const _method_desc = msg->GetMethodType();
585 local_endpoint->onResponse(_method_desc, std::move(msg));
586 }
587 else
588 {
589 bool needLocal = true;
590 if (msgInfo->futureInfo)
591 {
592 if (msgInfo->futureInfo(std::move(msg)))
593 {
594 needLocal = false;
595 }
596 }
597 if (needLocal)
598 {
599 local_endpoint->onResponse(msgInfo->method, std::move(msg));
600 }
601 d_ptr->removeRequestInfo(id);
602 }
603 }
604 else if (_kind == LspMessage::NOTIFICATION_MESSAGE)
605 {
606 if (strcmp(Notify_Cancellation::notify::kMethodInfo, msg->GetMethodType()) == 0)
607 {
608 d_ptr->onCancel(static_cast<Notify_Cancellation::notify*>(msg.get()));
609 }
610 else
611 {
612 local_endpoint->notify(std::move(msg));
613 }
614 }
615 else
616 {
617 std::string info = "Unknown lsp message when process message in mainLoop:\n";
618 d_ptr->log.log(Log::Level::WARNING, info);
619 }
620 }
621
622 void RemoteEndPoint::handle(std::vector<MessageIssue>&& issue)
623 {
624 for (auto& it : issue)
625 {
626 d_ptr->log.log(it.code, it.text);
627 }
628 }
629
630 void RemoteEndPoint::handle(MessageIssue&& issue)
631 {
632 d_ptr->log.log(issue.code, issue.text);
633 }
634
635 void RemoteEndPoint::startProcessingMessages(std::shared_ptr<lsp::istream> r, std::shared_ptr<lsp::ostream> w)
636 {
637 d_ptr->quit.store(false, std::memory_order_relaxed);
638 d_ptr->input = r;
639 d_ptr->output = w;
640 d_ptr->message_producer->bind(r);
641 d_ptr->tp = std::make_shared<asio::thread_pool>(d_ptr->max_workers);
642 message_producer_thread_ = std::make_shared<std::thread>(
643 [&]()
644 {
645 d_ptr->message_producer->listen(
646 [&](std::string&& content)
647 {
648 auto const temp = std::make_shared<std::string>(std::move(content));
649 asio::post(
650 *d_ptr->tp,
651 [this, temp]
652 {
653 #ifdef LSPCPP_USEGC
654 GCThreadContext gcContext;
655 #endif
656
657 dispatch(*temp);
658 }
659 );
660 }
661 );
662 }
663 );
664 }
665
666 void RemoteEndPoint::stop()
667 {
668 if (message_producer_thread_ && message_producer_thread_->joinable())
669 {
670 message_producer_thread_->detach();
671 message_producer_thread_ = nullptr;
672 }
673 d_ptr->clear();
674 }
675
676 void RemoteEndPoint::sendMsg(LspMessage& msg)
677 {
678
679 std::lock_guard<std::mutex> lock(m_sendMutex);
680 if (!d_ptr->output || d_ptr->output->bad())
681 {
682 std::string info = "Output isn't good any more:\n";
683 d_ptr->log.log(Log::Level::INFO, info);
684 return;
685 }
686 WriterMsg(d_ptr->output, msg);
687 }
688
689 bool RemoteEndPoint::isWorking() const
690 {
691 if (message_producer_thread_ && message_producer_thread_->joinable())
692 {
693 return true;
694 }
695 return false;
696 }