summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsamantharitter <samantha.ritter@10gen.com>2017-06-02 13:36:44 -0400
committersamantharitter <samantha.ritter@10gen.com>2017-06-05 13:29:41 -0400
commit558878c94230c19dbe86d397e78fbeb7389667c9 (patch)
treea2c3f28fc99e725cea096c7fd4ce090a1fffeeb0
parentafa4f0334edc8e88b33ca52a56cc6bb1f109b953 (diff)
downloadmongo-558878c94230c19dbe86d397e78fbeb7389667c9.tar.gz
SERVER-28300 Make PeriodicRunnerASIO more resilient
-rw-r--r--src/mongo/util/periodic_runner.h2
-rw-r--r--src/mongo/util/periodic_runner_asio.cpp10
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 <algorithm>
#include <memory>
#include "mongo/util/periodic_runner_asio.h"
@@ -56,10 +57,10 @@ void PeriodicRunnerASIO::scheduleJob(PeriodicJob job) {
std::shared_ptr<executor::AsyncTimerInterface> timer{std::move(uniqueTimer)};
auto asioJob = std::make_shared<PeriodicJobASIO>(std::move(job), _timerFactory->now(), timer);
- _jobs.insert(_jobs.end(), asioJob);
{
stdx::unique_lock<stdx::mutex> lk(_stateMutex);
+ _jobs.insert(_jobs.end(), asioJob);
if (_state == State::kRunning) {
_scheduleJob(asioJob);
}
@@ -73,7 +74,8 @@ void PeriodicRunnerASIO::_scheduleJob(std::weak_ptr<PeriodicJobASIO> 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();
}
}