Commit 9616c483 authored by Subodh Iyengar's avatar Subodh Iyengar Committed by Facebook Github Bot

Fix de-ref of evb after evb deletion

Summary:
HHWheelTimer is a member of EventBase, and EventBase is passed to it in the ctor.
However during the destruction of the evb, wheel timer gets destroyed after the evb is destroyed and still retains a reference to the evb. This is not so good.

This diff fixes it by canceling the timer before the evb gets destroyed.

Reviewed By: lnicco

Differential Revision: D15177816

fbshipit-source-id: 7298fcdc3a73041484a315c420bcb866175c19cc
parent 967e3cae
...@@ -163,6 +163,10 @@ EventBase::~EventBase() { ...@@ -163,6 +163,10 @@ EventBase::~EventBase() {
virtualEventBaseDestroyFuture.get(); virtualEventBaseDestroyFuture.get();
} }
// Destruction of the wheel timer may trigger cancellation callbacks.
// Run those before destruction callbacks.
wheelTimer_.reset();
// Call all destruction callbacks, before we start cleaning up our state. // Call all destruction callbacks, before we start cleaning up our state.
while (!onDestructionCallbacks_.rlock()->empty()) { while (!onDestructionCallbacks_.rlock()->empty()) {
OnDestructionCallback::List callbacks; OnDestructionCallback::List callbacks;
......
...@@ -2129,3 +2129,28 @@ TEST(EventBaseTest, RunOnDestructionAddCallbackWithinCallback) { ...@@ -2129,3 +2129,28 @@ TEST(EventBaseTest, RunOnDestructionAddCallbackWithinCallback) {
} }
EXPECT_EQ(2, callbacksCalled); EXPECT_EQ(2, callbacksCalled);
} }
class TestWheelTimeout : public HHWheelTimer::Callback {
public:
~TestWheelTimeout() override = default;
void callbackCanceled() noexcept override {
// This is invoked when the EventBase is destroyed
canceled = true;
}
void timeoutExpired() noexcept override {}
bool canceled{false};
};
TEST(EventBaseTest, WheelTimerWhileDestroyingEvb) {
TestWheelTimeout timeout;
{
EventBase eb;
eb.runOnDestruction([&]() { EXPECT_TRUE(timeout.canceled); });
auto& timer = eb.timer();
timer.scheduleTimeout(&timeout, std::chrono::seconds(10));
}
}
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