From 9ae1671e4f53e2767ba6216a9212f2ffd0997ced Mon Sep 17 00:00:00 2001 From: Tyler Neely Date: Wed, 20 Jul 2022 18:17:06 +0000 Subject: [PATCH] Fix bug with MgFuture where its safety check was not correctly initialized --- src/io/v3/future.hpp | 48 ++++++++++++++++------------------ src/io/v3/simulator_handle.hpp | 1 + tests/simulation/raft.cpp | 2 +- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/io/v3/future.hpp b/src/io/v3/future.hpp index 55b984ba2..dd909cbc4 100644 --- a/src/io/v3/future.hpp +++ b/src/io/v3/future.hpp @@ -36,21 +36,18 @@ std::pair, MgPromise> FuturePromisePairWithNotifier(std::function template class Shared { + std::condition_variable cv_; + std::mutex mu_; + std::optional item_; + bool consumed_ = false; + bool waiting_ = false; + std::optional> simulator_notifier_; + friend std::pair, MgPromise> FuturePromisePair(); friend std::pair, MgPromise> FuturePromisePairWithNotifier(std::function); friend MgPromise; friend MgFuture; - public: - Shared(std::function simulator_notifier) : simulator_notifier_(simulator_notifier) {} - Shared() = default; - Shared(Shared &&) = delete; - Shared &operator=(Shared &&) = delete; - Shared(const Shared &) = delete; - Shared &operator=(const Shared &) = delete; - ~Shared() = default; - - private: T Wait() { std::unique_lock lock(mu_); waiting_ = true; @@ -132,16 +129,22 @@ class Shared { return waiting_; } - std::condition_variable cv_; - std::mutex mu_; - std::optional item_; - bool consumed_; - bool waiting_; - std::optional> simulator_notifier_; + public: + Shared(std::function simulator_notifier) : simulator_notifier_(simulator_notifier) {} + Shared() = default; + Shared(Shared &&) = delete; + Shared &operator=(Shared &&) = delete; + Shared(const Shared &) = delete; + Shared &operator=(const Shared &) = delete; + ~Shared() = default; }; template class MgFuture { + MgFuture(std::shared_ptr> shared) : shared_(shared) {} + + bool consumed_or_moved_ = false; + std::shared_ptr> shared_; friend std::pair, MgPromise> FuturePromisePair(); friend std::pair, MgPromise> FuturePromisePairWithNotifier(std::function); @@ -196,16 +199,13 @@ class MgFuture { MG_ASSERT(!consumed_or_moved_, "MgFuture::Cancel called on a future that was already moved or consumed!"); consumed_or_moved_ = true; } - - private: - MgFuture(std::shared_ptr> shared) : shared_(shared) {} - - bool consumed_or_moved_ = false; - std::shared_ptr> shared_; }; template class MgPromise { + std::shared_ptr> shared_; + bool filled_or_moved_ = false; + friend std::pair, MgPromise> FuturePromisePair(); friend std::pair, MgPromise> FuturePromisePairWithNotifier(std::function); @@ -244,10 +244,6 @@ class MgPromise { return up; } - - private: - std::shared_ptr> shared_; - bool filled_or_moved_ = false; }; template diff --git a/src/io/v3/simulator_handle.hpp b/src/io/v3/simulator_handle.hpp index 109b636f0..7a45c9980 100644 --- a/src/io/v3/simulator_handle.hpp +++ b/src/io/v3/simulator_handle.hpp @@ -256,6 +256,7 @@ class SimulatorHandle { for (auto &[promise_key, dop] : promises_) { // TODO queue this up and drop it after its deadline if (dop.deadline < now) { + std::cout << "timing out request" << std::endl; DeadlineAndOpaquePromise dop = std::move(promises_.at(promise_key)); promises_.erase(promise_key); dop.promise.TimeOut(); diff --git a/tests/simulation/raft.cpp b/tests/simulation/raft.cpp index df0c2869b..aa6e8bd5a 100644 --- a/tests/simulation/raft.cpp +++ b/tests/simulation/raft.cpp @@ -627,7 +627,7 @@ void RunSimulation() { .perform_timeouts = true, .scramble_messages = true, .rng_seed = 0, - .start_time = 200000, + .start_time = 256 * 1024, .abort_time = 8 * 1024 * 1024, };