From 558878c94230c19dbe86d397e78fbeb7389667c9 Mon Sep 17 00:00:00 2001 From: samantharitter Date: Fri, 2 Jun 2017 13:36:44 -0400 Subject: SERVER-28300 Make PeriodicRunnerASIO more resilient --- src/mongo/util/periodic_runner.h | 2 ++ src/mongo/util/periodic_runner_asio.cpp | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/mongo/util/periodic_runner.h b/src/mongo/util/periodic_runner.h index eaf5170b5cd..21e37d0e438 100644 --- a/src/mongo/util/periodic_runner.h +++ b/src/mongo/util/periodic_runner.h @@ -26,6 +26,8 @@ * then also delete it in the license file. */ +#pragma once + #include "mongo/base/disallow_copying.h" #include "mongo/stdx/functional.h" #include "mongo/util/time_support.h" diff --git a/src/mongo/util/periodic_runner_asio.cpp b/src/mongo/util/periodic_runner_asio.cpp index 1fd59ede31c..f1648b090cf 100644 --- a/src/mongo/util/periodic_runner_asio.cpp +++ b/src/mongo/util/periodic_runner_asio.cpp @@ -30,6 +30,7 @@ #include "mongo/platform/basic.h" +#include #include #include "mongo/util/periodic_runner_asio.h" @@ -56,10 +57,10 @@ void PeriodicRunnerASIO::scheduleJob(PeriodicJob job) { std::shared_ptr timer{std::move(uniqueTimer)}; auto asioJob = std::make_shared(std::move(job), _timerFactory->now(), timer); - _jobs.insert(_jobs.end(), asioJob); { stdx::unique_lock lk(_stateMutex); + _jobs.insert(_jobs.end(), asioJob); if (_state == State::kRunning) { _scheduleJob(asioJob); } @@ -73,7 +74,8 @@ void PeriodicRunnerASIO::_scheduleJob(std::weak_ptr job) { } // Adjust the timer to expire at the correct time. - auto adjustedMS = lockedJob->start + lockedJob->interval - _timerFactory->now(); + auto adjustedMS = + std::max(Milliseconds(0), lockedJob->start + lockedJob->interval - _timerFactory->now()); lockedJob->timer->expireAfter(adjustedMS); lockedJob->timer->asyncWait([this, job](std::error_code ec) mutable { if (ec) { @@ -135,8 +137,10 @@ void PeriodicRunnerASIO::shutdown() { _state = State::kComplete; _io_service.stop(); - _jobs.clear(); + + lk.unlock(); + _thread.join(); } } -- cgit v1.2.1