Handle timekeeperSingleton being nullptr in within()
authorAlex Yarmula <ayarmula@fb.com>
Thu, 19 Oct 2017 20:35:27 +0000 (13:35 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 19 Oct 2017 20:47:10 +0000 (13:47 -0700)
Summary: When timekeeper singleton no longer exists during shutdown and folly::Singleton::try_get() can return nullptr, make sure nullptr is handled gracefully.

Reviewed By: yfeldblum

Differential Revision: D6101311

fbshipit-source-id: fefeddfbd048d1a7632688bb3526db15b685dd72

folly/futures/Future-inl.h
folly/futures/test/TimekeeperTest.cpp

index 70a2c58..c7ddeb0 100644 (file)
@@ -1218,9 +1218,13 @@ Future<T> Future<T>::within(Duration dur, E e, Timekeeper* tk) {
   }
 
   std::shared_ptr<Timekeeper> tks;
-  if (!tk) {
+  if (LIKELY(!tk)) {
     tks = folly::detail::getTimekeeperSingleton();
-    tk = DCHECK_NOTNULL(tks.get());
+    tk = tks.get();
+  }
+
+  if (UNLIKELY(!tk)) {
+    return makeFuture<T>(NoTimekeeper());
   }
 
   auto ctx = std::make_shared<Context>(std::move(e));
index 4567b93..fdf17ed 100644 (file)
@@ -88,6 +88,16 @@ TEST(Timekeeper, futureSleepHandlesNullTimekeeperSingleton) {
   EXPECT_THROW(futures::sleep(one_ms).get(), NoTimekeeper);
 }
 
+TEST(Timekeeper, futureWithinHandlesNullTimekeeperSingleton) {
+  Singleton<ThreadWheelTimekeeper>::make_mock([] { return nullptr; });
+  SCOPE_EXIT {
+    Singleton<ThreadWheelTimekeeper>::make_mock();
+  };
+  Promise<int> p;
+  auto f = p.getFuture().within(one_ms);
+  EXPECT_THROW(f.get(), NoTimekeeper);
+}
+
 TEST(Timekeeper, futureDelayed) {
   auto t1 = now();
   auto dur = makeFuture()