From abb0fcb2035fa96b1ec09f9480838c476527ccd4 Mon Sep 17 00:00:00 2001 From: Denis Krjuchkov Date: Sun, 13 Jan 2013 15:20:32 +0600 Subject: Win32Main.cxx: more clean shutdown This change fixes two issues: 1) console_handler is called from a separate thread. Thread-safe primitive is required for correct operation. 2) If console_handler returns TRUE our process is immediately terminated. We use Sleep() to give main thread an opportunity to shutdown correctly. --- src/Win32Main.cxx | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/Win32Main.cxx') diff --git a/src/Win32Main.cxx b/src/Win32Main.cxx index 8932e8ed..0cd5e445 100644 --- a/src/Win32Main.cxx +++ b/src/Win32Main.cxx @@ -25,6 +25,9 @@ #include "mpd_error.h" #include "GlobalEvents.hxx" +#include +#include + #include #include @@ -32,7 +35,7 @@ static int service_argc; static char **service_argv; static char service_name[] = ""; -static BOOL ignore_console_events; +static std::atomic_bool running; static SERVICE_STATUS_HANDLE service_handle; static void WINAPI @@ -103,8 +106,22 @@ console_handler(DWORD event) switch (event) { case CTRL_C_EVENT: case CTRL_CLOSE_EVENT: - if (!ignore_console_events) + if (running.load()) { + // Recent msdn docs that process is terminated + // if this function returns TRUE. + // We initiate correct shutdown sequence (if possible). + // Once main() returns CRT will terminate our process + // regardless our thread is still active. + // If this did not happen within 3 seconds + // let's shutdown anyway. GlobalEvents::Emit(GlobalEvents::SHUTDOWN); + // Under debugger it's better to wait indefinitely + // to allow debugging of shutdown code. + Sleep(IsDebuggerPresent() ? INFINITE : 3000); + } + // If we're not running main loop there is no chance for + // clean shutdown. + std::exit(EXIT_FAILURE); return TRUE; default: return FALSE; @@ -125,8 +142,8 @@ int win32_main(int argc, char *argv[]) error_code = GetLastError(); if (error_code == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { /* running as console app */ + running.store(false); SetConsoleTitle("Music Player Daemon"); - ignore_console_events = TRUE; SetConsoleCtrlHandler(console_handler, TRUE); return mpd_main(argc, argv); } @@ -140,7 +157,7 @@ void win32_app_started() if (service_handle != 0) service_notify_status(SERVICE_RUNNING); else - ignore_console_events = FALSE; + running.store(true); } void win32_app_stopping() @@ -148,7 +165,7 @@ void win32_app_stopping() if (service_handle != 0) service_notify_status(SERVICE_STOP_PENDING); else - ignore_console_events = TRUE; + running.store(false); } #endif -- cgit v1.2.3