fix(daemon): Use scheduled state when start is scheduled

This commit is contained in:
Nick Bolton 2025-04-15 23:14:58 +01:00 committed by Chris Rizzitello
parent abc963f886
commit 1202145bb8
2 changed files with 14 additions and 14 deletions

View file

@ -177,6 +177,8 @@ MSWindowsWatchdog::getUserToken(LPSECURITY_ATTRIBUTES security, bool elevatedTok
void MSWindowsWatchdog::mainLoop(void *) void MSWindowsWatchdog::mainLoop(void *)
{ {
using enum ProcessState;
shutdownExistingProcesses(); shutdownExistingProcesses();
LOG_DEBUG("starting watchdog main loop"); LOG_DEBUG("starting watchdog main loop");
@ -184,15 +186,13 @@ void MSWindowsWatchdog::mainLoop(void *)
LOG_DEBUG3("locking process state mutex in watchdog main loop"); LOG_DEBUG3("locking process state mutex in watchdog main loop");
std::unique_lock lock(m_processStateMutex); std::unique_lock lock(m_processStateMutex);
if (!m_command.empty() && !m_foreground && m_session.hasChanged()) { if (m_processState == Running && !m_command.empty() && !m_foreground && m_session.hasChanged()) {
LOG_DEBUG("session changed, queueing process start"); LOG_DEBUG("session changed, queueing process start");
m_processState = ProcessState::StartPending; m_processState = StartPending;
m_nextStartTime.reset(); m_nextStartTime.reset();
} }
switch (m_processState) { switch (m_processState) {
using enum ProcessState;
case Idle: case Idle:
LOG_DEBUG3("watchdog process state idle"); LOG_DEBUG3("watchdog process state idle");
break; break;
@ -212,11 +212,9 @@ void MSWindowsWatchdog::mainLoop(void *)
m_startFailures = 0; m_startFailures = 0;
m_processState = Running; m_processState = Running;
} catch (std::exception &e) { // NOSONAR - Catching all exceptions } catch (std::exception &e) { // NOSONAR - Catching all exceptions
handleStartError(e.what()); m_processState = handleStartError(e.what());
m_processState = StartPending;
} catch (...) { // NOSONAR - Catching remaining exceptions } catch (...) { // NOSONAR - Catching remaining exceptions
handleStartError(); m_processState = handleStartError();
m_processState = StartPending;
} }
} break; } break;
@ -313,10 +311,10 @@ void MSWindowsWatchdog::startProcess()
if (!createRet) { if (!createRet) {
DWORD exitCode = 0; DWORD exitCode = 0;
if (GetExitCodeProcess(m_process->info().hProcess, &exitCode)) { if (GetExitCodeProcess(m_process->info().hProcess, &exitCode)) {
LOG_ERR("daemon failed to run command, exit code: %d", exitCode); LOG_ERR("daemon failed to run command, exit code: %d", exitCode);
} else { } else {
LOG_ERR("daemon failed to run command, unknown exit code"); LOG_ERR("daemon failed to run command, unknown exit code");
throw XArch(new XArchEvalWindows); throw XArch(new XArchEvalWindows);
} }
} else { } else {
// Wait for program to fail. This needs to be 1 second, as the process may take some time to fail. // Wait for program to fail. This needs to be 1 second, as the process may take some time to fail.
@ -487,16 +485,16 @@ std::string MSWindowsWatchdog::runActiveDesktopUtility()
return output; return output;
} }
void MSWindowsWatchdog::handleStartError(const std::string_view &message) MSWindowsWatchdog::ProcessState MSWindowsWatchdog::handleStartError(const std::string_view &message)
{ {
const auto kStartDelaySeconds = 1; const auto kStartDelaySeconds = 1;
m_startFailures++; m_startFailures++;
if (!message.empty()) { if (!message.empty()) {
LOG_CRIT("failed to launch, error: %s", message.data()); LOG_CRIT("daemon failed to start process, error: %s", message.data());
} else { } else {
LOG_CRIT("failed to launch, unknown error"); LOG_CRIT("daemon failed to start process, unknown error");
} }
// When there has been more than one consecutive failure, slow down the retry rate. // When there has been more than one consecutive failure, slow down the retry rate.
@ -504,9 +502,11 @@ void MSWindowsWatchdog::handleStartError(const std::string_view &message)
m_nextStartTime = ARCH->time() + kStartDelaySeconds; m_nextStartTime = ARCH->time() + kStartDelaySeconds;
LOG_WARN("start failed %d times, delaying start", m_startFailures); LOG_WARN("start failed %d times, delaying start", m_startFailures);
LOG_DEBUG("start delay, seconds=%d, time=%f", kStartDelaySeconds, m_nextStartTime.value()); LOG_DEBUG("start delay, seconds=%d, time=%f", kStartDelaySeconds, m_nextStartTime.value());
return ProcessState::StartScheduled;
} else { } else {
LOG_INFO("retrying process start immediately"); LOG_INFO("retrying process start immediately");
m_nextStartTime.reset(); m_nextStartTime.reset();
return ProcessState::StartPending;
} }
} }

View file

@ -99,7 +99,7 @@ private:
/** /**
* @brief Controls whether the process should restart immediately or delay start. * @brief Controls whether the process should restart immediately or delay start.
*/ */
void handleStartError(const std::string_view &message = ""); ProcessState handleStartError(const std::string_view &message = "");
/** /**
* @brief Init the output read pipe for standard out/error. * @brief Init the output read pipe for standard out/error.