File: Shutdown.h

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,368 kB
  • sloc: cpp: 5,593,980; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (84 lines) | stat: -rw-r--r-- 3,605 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//===--- Shutdown.h - Unclean exit scenarios --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// LSP specifies a protocol for shutting down: a `shutdown` request followed
// by an `exit` notification. If this protocol is followed, clangd should
// finish outstanding work and exit with code 0.
//
// The way this works in the happy case:
//  - when ClangdLSPServer gets `shutdown`, it sets a flag
//  - when ClangdLSPServer gets `exit`, it returns false to indicate end-of-LSP
//  - Transport::loop() returns with no error
//  - ClangdServer::run() checks the shutdown flag and returns with no error.
//  - we `return 0` from main()
//  - destructor of ClangdServer and other main()-locals runs.
//    This blocks until outstanding requests complete (results are ignored)
//  - global destructors run, such as fallback deletion of temporary files
//
// There are a number of things that can go wrong. Some are handled here, and
// some elsewhere.
//  - `exit` notification with no `shutdown`:
//    ClangdServer::run() sees this and returns false, main() returns nonzero.
//  - stdin/stdout are closed
//    The Transport detects this while doing IO and returns an error from loop()
//    ClangdServer::run() logs a message and then returns false, etc
//  - a request thread gets stuck, so the ClangdServer destructor hangs.
//    Before returning from main(), we start a watchdog thread to abort() the
//    process if it takes too long to exit. See abortAfterTimeout().
//  - clangd crashes (e.g. segfault or assertion)
//    A fatal signal is sent (SEGV, ABRT, etc)
//    The installed signal handler prints a stack trace and exits.
//  - parent process goes away or tells us to shut down
//    A "graceful shutdown" signal is sent (TERM, HUP, etc).
//    The installed signal handler calls requestShutdown() which sets a flag.
//    The Transport IO is interrupted, and Transport::loop() checks the flag and
//    returns an error, etc.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_SHUTDOWN_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_SHUTDOWN_H

#include <cerrno>
#include <chrono>

namespace clang {
namespace clangd {

/// Causes this process to crash if still running after Timeout.
void abortAfterTimeout(std::chrono::seconds Timeout);

/// Sets a flag to indicate that clangd was sent a shutdown signal, and the
/// transport loop should exit at the next opportunity.
/// If shutdown was already requested, aborts the process.
/// This function is threadsafe and signal-safe.
void requestShutdown();
/// Checks whether requestShutdown() was called.
/// This function is threadsafe and signal-safe.
bool shutdownRequested();

/// Retry an operation if it gets interrupted by a signal.
/// This is like llvm::sys::RetryAfterSignal, except that if shutdown was
/// requested (which interrupts IO), we'll fail rather than retry.
template <typename Fun, typename Ret = decltype(std::declval<Fun>()())>
Ret retryAfterSignalUnlessShutdown(
    const std::enable_if_t<true, Ret> &Fail, // Suppress deduction.
    const Fun &F) {
  Ret Res;
  do {
    if (shutdownRequested())
      return Fail;
    errno = 0;
    Res = F();
  } while (Res == Fail && errno == EINTR);
  return Res;
}

} // namespace clangd
} // namespace clang

#endif