Commit 261d05eb authored by Misha Shneerson's avatar Misha Shneerson Committed by Facebook GitHub Bot

Fix EventBaseLocal destruction

Summary: When EventBase is destructed, the underlying local storage should be destructed prior to event loop being destroyed.

Reviewed By: andriigrynenko

Differential Revision: D26583298

fbshipit-source-id: 9bfb8442b2a827aa2b3564b27ea765ec1e7b26a2
parent 941f7020
......@@ -220,11 +220,13 @@ EventBase::~EventBase() {
// Stop consumer before deleting NotificationQueue
queue_->stopConsuming();
evb_.reset();
for (auto storage : localStorageToDtor_) {
storage->onEventBaseDestruction(*this);
}
localStorage_.clear();
evb_.reset();
VLOG(5) << "EventBase(): Destroyed.";
}
......
......@@ -16,6 +16,7 @@
#include <folly/io/async/EventBaseLocal.h>
#include <folly/io/async/EventBaseAtomicNotificationQueue.h>
#include <folly/portability/GTest.h>
struct Foo {
......@@ -90,3 +91,24 @@ TEST(EventBaseLocalTest, emplaceNoncopyable) {
ints.emplace(evb, std::make_unique<int>(42));
EXPECT_EQ(42, **ints.get(evb));
}
TEST(EventBaseLocalTest, DestructionOrder) {
struct Consumer {
void operator()(int) noexcept {}
};
using Queue = folly::EventBaseAtomicNotificationQueue<int, Consumer>;
folly::EventBaseLocal<std::unique_ptr<Queue>> ebl;
{
// Since queue binds to the underlying event loop, we must ensure
// local storage is cleared before event loop is destroyed.
folly::EventBase evb;
ebl.emplace_with(
evb,
[&evb] {
auto q = std::make_unique<Queue>();
q->startConsumingInternal(&evb);
return q;
})
.get();
}
}
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