# Rocksdb Change Log
+## Unreleased
+### Bug Fixes
+* Fix a race condition between DB startups and shutdowns in managing the periodic background worker threads. One effect of this race condition could be the process being terminated.
+
## 6.15.3 (01/07/2021)
### Bug Fixes
* For Java builds, fix errors due to missing compression library includes.
#ifndef ROCKSDB_LITE
namespace ROCKSDB_NAMESPACE {
-PeriodicWorkScheduler::PeriodicWorkScheduler(Env* env) {
+PeriodicWorkScheduler::PeriodicWorkScheduler(Env* env) : timer_mu_(env) {
timer = std::unique_ptr<Timer>(new Timer(env));
}
void PeriodicWorkScheduler::Register(DBImpl* dbi,
unsigned int stats_dump_period_sec,
unsigned int stats_persist_period_sec) {
+ MutexLock l(&timer_mu_);
static std::atomic<uint64_t> initial_delay(0);
timer->Start();
if (stats_dump_period_sec > 0) {
}
void PeriodicWorkScheduler::Unregister(DBImpl* dbi) {
+ MutexLock l(&timer_mu_);
timer->Cancel(GetTaskName(dbi, "dump_st"));
timer->Cancel(GetTaskName(dbi, "pst_st"));
timer->Cancel(GetTaskName(dbi, "flush_info_log"));
MutexLock l(&mutex);
if (scheduler.timer.get() != nullptr &&
scheduler.timer->TEST_GetPendingTaskNum() == 0) {
- scheduler.timer->Shutdown();
+ {
+ MutexLock timer_mu_guard(&scheduler.timer_mu_);
+ scheduler.timer->Shutdown();
+ }
scheduler.timer.reset(new Timer(env));
}
}
protected:
std::unique_ptr<Timer> timer;
+ // `timer_mu_` serves two purposes currently:
+ // (1) to ensure calls to `Start()` and `Shutdown()` are serialized, as
+ // they are currently not implemented in a thread-safe way; and
+ // (2) to ensure the `Timer::Add()`s and `Timer::Start()` run atomically, and
+ // the `Timer::Cancel()`s and `Timer::Shutdown()` run atomically.
+ port::Mutex timer_mu_;
explicit PeriodicWorkScheduler(Env* env);
// A Timer class to handle repeated work.
//
+// `Start()` and `Shutdown()` are currently not thread-safe. The client must
+// serialize calls to these two member functions.
+//
// A single timer instance can handle multiple functions via a single thread.
// It is better to leave long running work to a dedicated thread pool.
//