Commit cb65a6b2 authored by Chad Parry's avatar Chad Parry Committed by facebook-github-bot-4

Callbacks should ref the HHWheelTimer

Summary: Callbacks sometimes outlive the `HHWheelTimer` that they reference. Then the `Callback` tries to reference the dead `HHWheelTimer` and it could either misbehave or crash. This was caught reliably by ASAN tests.

Since `HHWheelTimer` already supports intrusive ref counting, the solution is to acquire a reference within the `Callback`.

Reviewed By: djwatson

Differential Revision: D2617966

fb-gh-sync-id: 02be9ffc5851c269d5933288a17ad394b33ac2dd
parent 06b04cb1
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <folly/io/async/HHWheelTimer.h> #include <folly/io/async/HHWheelTimer.h>
#include <folly/io/async/Request.h> #include <folly/io/async/Request.h>
#include <folly/Optional.h>
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <cassert> #include <cassert>
...@@ -54,6 +55,7 @@ void HHWheelTimer::Callback::setScheduled(HHWheelTimer* wheel, ...@@ -54,6 +55,7 @@ void HHWheelTimer::Callback::setScheduled(HHWheelTimer* wheel,
assert(wheel_ == nullptr); assert(wheel_ == nullptr);
assert(expiration_ == milliseconds(0)); assert(expiration_ == milliseconds(0));
wheelGuard_ = DestructorGuard(wheel);
wheel_ = wheel; wheel_ = wheel;
// Only update the now_ time if we're not in a timeout expired callback // Only update the now_ time if we're not in a timeout expired callback
...@@ -72,6 +74,7 @@ void HHWheelTimer::Callback::cancelTimeoutImpl() { ...@@ -72,6 +74,7 @@ void HHWheelTimer::Callback::cancelTimeoutImpl() {
hook_.unlink(); hook_.unlink();
wheel_ = nullptr; wheel_ = nullptr;
wheelGuard_ = folly::none;
expiration_ = milliseconds(0); expiration_ = milliseconds(0);
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#pragma once #pragma once
#include <folly/Optional.h>
#include <folly/io/async/AsyncTimeout.h> #include <folly/io/async/AsyncTimeout.h>
#include <folly/io/async/DelayedDestruction.h> #include <folly/io/async/DelayedDestruction.h>
...@@ -133,6 +134,7 @@ class HHWheelTimer : private folly::AsyncTimeout, ...@@ -133,6 +134,7 @@ class HHWheelTimer : private folly::AsyncTimeout,
void cancelTimeoutImpl(); void cancelTimeoutImpl();
HHWheelTimer* wheel_; HHWheelTimer* wheel_;
folly::Optional<DestructorGuard> wheelGuard_;
std::chrono::milliseconds expiration_; std::chrono::milliseconds expiration_;
typedef boost::intrusive::list_member_hook< typedef boost::intrusive::list_member_hook<
......
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