mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
merge bitcoin#20605: Signal-safe instant shutdown
This commit is contained in:
parent
3450cc755c
commit
94b706e402
@ -30,15 +30,6 @@
|
|||||||
|
|
||||||
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
||||||
|
|
||||||
static void WaitForShutdown(NodeContext& node)
|
|
||||||
{
|
|
||||||
while (!ShutdownRequested())
|
|
||||||
{
|
|
||||||
UninterruptibleSleep(std::chrono::milliseconds{200});
|
|
||||||
}
|
|
||||||
Interrupt(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// Start
|
// Start
|
||||||
@ -155,12 +146,10 @@ static bool AppInit(int argc, char* argv[])
|
|||||||
PrintExceptionContinue(std::current_exception(), "AppInit()");
|
PrintExceptionContinue(std::current_exception(), "AppInit()");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fRet)
|
if (fRet) {
|
||||||
{
|
WaitForShutdown();
|
||||||
Interrupt(node);
|
|
||||||
} else {
|
|
||||||
WaitForShutdown(node);
|
|
||||||
}
|
}
|
||||||
|
Interrupt(node);
|
||||||
Shutdown(node);
|
Shutdown(node);
|
||||||
|
|
||||||
return fRet;
|
return fRet;
|
||||||
|
@ -1229,6 +1229,9 @@ bool AppInitBasicSetup(const ArgsManager& args)
|
|||||||
// Enable heap terminate-on-corruption
|
// Enable heap terminate-on-corruption
|
||||||
HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);
|
HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);
|
||||||
#endif
|
#endif
|
||||||
|
if (!InitShutdownState()) {
|
||||||
|
return InitError(Untranslated("Initializing wait-for-shutdown state failed."));
|
||||||
|
}
|
||||||
|
|
||||||
if (!SetupNetworking()) {
|
if (!SetupNetworking()) {
|
||||||
return InitError(Untranslated("Initializing networking failed."));
|
return InitError(Untranslated("Initializing networking failed."));
|
||||||
|
@ -5,28 +5,120 @@
|
|||||||
|
|
||||||
#include <shutdown.h>
|
#include <shutdown.h>
|
||||||
|
|
||||||
|
#include <config/bitcoin-config.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <condition_variable>
|
||||||
|
#else
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
static std::atomic<bool> fRequestShutdown(false);
|
static std::atomic<bool> fRequestShutdown(false);
|
||||||
static std::atomic<bool> fRequestRestart(false);
|
static std::atomic<bool> fRequestRestart(false);
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
/** On windows it is possible to simply use a condition variable. */
|
||||||
|
std::mutex g_shutdown_mutex;
|
||||||
|
std::condition_variable g_shutdown_cv;
|
||||||
|
#else
|
||||||
|
/** On UNIX-like operating systems use the self-pipe trick.
|
||||||
|
* Index 0 will be the read end of the pipe, index 1 the write end.
|
||||||
|
*/
|
||||||
|
static int g_shutdown_pipe[2] = {-1, -1};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
bool InitShutdownState()
|
||||||
|
{
|
||||||
|
#ifndef WIN32
|
||||||
|
#if HAVE_O_CLOEXEC
|
||||||
|
// If we can, make sure that the file descriptors are closed on exec()
|
||||||
|
// to prevent interference.
|
||||||
|
if (pipe2(g_shutdown_pipe, O_CLOEXEC) != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if (pipe(g_shutdown_pipe) != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void StartShutdown()
|
void StartShutdown()
|
||||||
{
|
{
|
||||||
|
#ifdef WIN32
|
||||||
|
std::unique_lock<std::mutex> lk(g_shutdown_mutex);
|
||||||
fRequestShutdown = true;
|
fRequestShutdown = true;
|
||||||
|
g_shutdown_cv.notify_one();
|
||||||
|
#else
|
||||||
|
// This must be reentrant and safe for calling in a signal handler, so using a condition variable is not safe.
|
||||||
|
// Make sure that the token is only written once even if multiple threads call this concurrently or in
|
||||||
|
// case of a reentrant signal.
|
||||||
|
if (!fRequestShutdown.exchange(true)) {
|
||||||
|
// Write an arbitrary byte to the write end of the shutdown pipe.
|
||||||
|
const char token = 'x';
|
||||||
|
while (true) {
|
||||||
|
int result = write(g_shutdown_pipe[1], &token, 1);
|
||||||
|
if (result < 0) {
|
||||||
|
// Failure. It's possible that the write was interrupted by another signal.
|
||||||
|
// Other errors are unexpected here.
|
||||||
|
assert(errno == EINTR);
|
||||||
|
} else {
|
||||||
|
assert(result == 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
void StartRestart()
|
void StartRestart()
|
||||||
{
|
{
|
||||||
fRequestShutdown = fRequestRestart = true;
|
fRequestRestart = true;
|
||||||
|
StartShutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbortShutdown()
|
void AbortShutdown()
|
||||||
{
|
{
|
||||||
|
if (fRequestShutdown) {
|
||||||
|
// Cancel existing shutdown by waiting for it, this will reset condition flags and remove
|
||||||
|
// the shutdown token from the pipe.
|
||||||
|
WaitForShutdown();
|
||||||
|
}
|
||||||
fRequestShutdown = false;
|
fRequestShutdown = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ShutdownRequested()
|
bool ShutdownRequested()
|
||||||
{
|
{
|
||||||
return fRequestShutdown;
|
return fRequestShutdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RestartRequested()
|
bool RestartRequested()
|
||||||
{
|
{
|
||||||
return fRequestRestart;
|
return fRequestRestart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WaitForShutdown()
|
||||||
|
{
|
||||||
|
#ifdef WIN32
|
||||||
|
std::unique_lock<std::mutex> lk(g_shutdown_mutex);
|
||||||
|
g_shutdown_cv.wait(lk, [] { return fRequestShutdown.load(); });
|
||||||
|
#else
|
||||||
|
char token;
|
||||||
|
while (true) {
|
||||||
|
int result = read(g_shutdown_pipe[0], &token, 1);
|
||||||
|
if (result < 0) {
|
||||||
|
// Failure. Check if the read was interrupted by a signal.
|
||||||
|
// Other errors are unexpected here.
|
||||||
|
assert(errno == EINTR);
|
||||||
|
} else {
|
||||||
|
assert(result == 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
@ -6,10 +6,29 @@
|
|||||||
#ifndef BITCOIN_SHUTDOWN_H
|
#ifndef BITCOIN_SHUTDOWN_H
|
||||||
#define BITCOIN_SHUTDOWN_H
|
#define BITCOIN_SHUTDOWN_H
|
||||||
|
|
||||||
|
/** Initialize shutdown state. This must be called before using either StartShutdown(),
|
||||||
|
* AbortShutdown() or WaitForShutdown(). Calling ShutdownRequested() is always safe.
|
||||||
|
*/
|
||||||
|
bool InitShutdownState();
|
||||||
|
|
||||||
|
/** Request shutdown of the application. */
|
||||||
void StartShutdown();
|
void StartShutdown();
|
||||||
|
|
||||||
|
/** Request restart of the application. */
|
||||||
void StartRestart();
|
void StartRestart();
|
||||||
|
|
||||||
|
/** Clear shutdown flag. Only use this during init (before calling WaitForShutdown in any
|
||||||
|
* thread), or in the unit tests. Calling it in other circumstances will cause a race condition.
|
||||||
|
*/
|
||||||
void AbortShutdown();
|
void AbortShutdown();
|
||||||
|
|
||||||
|
/** Returns true if a shutdown is requested, false otherwise. */
|
||||||
bool ShutdownRequested();
|
bool ShutdownRequested();
|
||||||
bool RestartRequested();
|
bool RestartRequested();
|
||||||
|
|
||||||
|
/** Wait for StartShutdown to be called in any thread. This can only be used
|
||||||
|
* from a single thread.
|
||||||
|
*/
|
||||||
|
void WaitForShutdown();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user