From a742e64e4d0bd7030746c3d409865808307e7ad4 Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Wed, 14 Mar 2018 10:01:11 +0100 Subject: [PATCH] Fix reload handling Make sure we have written the new PID before letting it take over. --- lib/base/application.cpp | 34 +++++++++++++++------------------- lib/cli/daemoncommand.cpp | 9 ++++++++- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 6f0f9d660..f28946abd 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -90,25 +90,7 @@ void Application::Stop(bool runtimeRemoved) WSACleanup(); #endif /* _WIN32 */ - // Getting a shutdown-signal when a restart is in progress usually - // means that the restart succeeded and the new process wants to take - // over. Write the PID of the new process to the pidfile before this - // process exits to keep systemd happy. - if (l_Restarting) { - try { - UpdatePidFile(GetPidPath(), m_ReloadProcess); - } catch (const std::exception&) { - /* abort restart */ - Log(LogCritical, "Application", "Cannot update PID file. Aborting restart operation."); - return; - } - - Log(LogDebug, "Application") - << "Keeping pid '" << m_ReloadProcess << "' open."; - - ClosePidFile(false); - } else - ClosePidFile(true); + ClosePidFile(true); ObjectImpl::Stop(runtimeRemoved); } @@ -741,6 +723,20 @@ void Application::SigUsr2Handler(int) sd_notifyf(0, "MAINPID=%lu", (unsigned long) m_ReloadProcess); #endif /* HAVE_SYSTEMD */ + /* Write the PID of the new process to the pidfile before this + * process exits to keep systemd happy. + */ + Application::Ptr instance = GetInstance(); + try { + instance->UpdatePidFile(GetPidPath(), m_ReloadProcess); + } catch (const std::exception&) { + /* abort restart */ + Log(LogCritical, "Application", "Cannot update PID file. Aborting restart operation."); + return; + } + + instance->ClosePidFile(false); + Exit(0); } diff --git a/lib/cli/daemoncommand.cpp b/lib/cli/daemoncommand.cpp index 199fa67ff..5b34bc18d 100644 --- a/lib/cli/daemoncommand.cpp +++ b/lib/cli/daemoncommand.cpp @@ -262,10 +262,17 @@ int DaemonCommand::Run(const po::variables_map& vm, const std::vector(), SIGUSR2); if (rc) { - Log(LogCritical, "Application") + Log(LogCritical, "cli") << "Failed to send signal to \"" << vm["reload-internal"].as() << "\" with " << strerror(errno); return EXIT_FAILURE; } + + double start = Utility::GetTime(); + while (kill(vm["reload-internal"].as(), SIGCHLD) == 0) + Utility::Sleep(0.2); + + Log(LogNotice, "cli") + << "Waited for " << Utility::FormatDuration(Utility::GetTime() - start) << " on old process to exit."; } #endif /* _WIN32 */ -- 2.40.0