Commit 0ba2ee1f authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook GitHub Bot

fix a crash in AsyncSignalHandler destruction

Summary:
Update AsyncSignalHandler to check if the events are still registered before
trying to unregister them during destruction.

Without this check the code crashes if an AsyncSignalHandler is registered,
and then the EventBase is destroyed before the AsyncSignalHandler is
destroyed.  When the EventBase is destroyed the AsyncSignalHandler's event
will be automatically unregistered, so `eb_event_del()` should not be called
again here, and calling it triggers a crash by attempting to dereference the
now-destroyed EventBase.

This change makes AsyncSignalHandler behave more like the AsyncTimeout and
EventHandler objects, which both support destroying the EventBase before these
objects.

Differential Revision: D25837541

fbshipit-source-id: a1c44ab442b9078bf84a51be2cced432d247f6cd
parent 99330de1
......@@ -31,7 +31,10 @@ AsyncSignalHandler::AsyncSignalHandler(EventBase* eventBase)
AsyncSignalHandler::~AsyncSignalHandler() {
// Unregister any outstanding events
for (auto& signalEvent : signalEvents_) {
signalEvent.second->eb_event_del();
// isEventRegistered() may be false if the EventBase was destroyed before us
if (signalEvent.second->isEventRegistered()) {
signalEvent.second->eb_event_del();
}
}
}
......
......@@ -28,5 +28,18 @@ INSTANTIATE_TYPED_TEST_CASE_P(
AsyncSignalHandlerTest,
AsyncSignalHandlerTest,
DefaultBackendProvider);
TEST(AsyncSignalHandler, destructionOrder) {
auto evb = std::make_unique<EventBase>();
TestSignalHandler handler(evb.get());
handler.registerSignalHandler(SIGHUP);
// Test a situation where the EventBase is destroyed with an
// AsyncSignalHandler still installed. Destroying the AsyncSignalHandler
// after the EventBase should work normally and should not crash or access
// invalid memory.
evb.reset();
}
} // namespace test
} // namespace folly
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