Commit e45360b5 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Fix when the Timekeeper is destroyed with in-flight delays

Summary:
[Folly] Fix when the Timekeeper is destroyed with in-flight delays.

Previously, the Timekeeper dtor would immediately trigger the delayed callbacks early with a success result. But there are various situations where that is wrong behavior, such as when the callback enqueues more delayed callbacks in the success path. Higher-level, a future returned by `Timekeeper::after` or `sleep` would be completed early with a value.

Now, let it immediately trigger the delayed callbacks early with an error result. Higher-level, a future returned by `Timekeeper::after` or `sleep` will be completed early with an error.

Also: use `NoTimeout` consistently, and, on interrupt, pass the interrupt exception through.

Differential Revision: D6837669

fbshipit-source-id: 7d18d7f57999d56bbb5269cacdb443ac22448cf0
parent e7c9b18f
......@@ -67,6 +67,14 @@ struct WTCallback : public std::enable_shared_from_this<WTCallback>,
}
}
void callbackCanceled() noexcept override {
// Don't need Promise anymore, break the circular reference
auto promise = stealPromise();
if (!promise.isFulfilled()) {
promise.setException(NoTimekeeper{});
}
}
void interruptHandler(exception_wrapper ew) {
// Capture shared_ptr of self in lambda, if we don't do this, object
// may go away before the lambda is executed from event base thread.
......
......@@ -253,3 +253,13 @@ TEST_F(TimekeeperFixture, howToCastDuration) {
auto f = timeLord_->after(std::chrono::duration_cast<Duration>(
std::chrono::nanoseconds(1)));
}
TEST_F(TimekeeperFixture, destruction) {
folly::Optional<ThreadWheelTimekeeper> tk;
tk.emplace();
auto f = tk->after(std::chrono::seconds(10));
EXPECT_FALSE(f.isReady());
tk.clear();
EXPECT_TRUE(f.isReady());
EXPECT_TRUE(f.hasException());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment