diff --git a/source/common/event/dispatcher_impl.cc b/source/common/event/dispatcher_impl.cc index 0e208959eec24..d609d9bba9e36 100644 --- a/source/common/event/dispatcher_impl.cc +++ b/source/common/event/dispatcher_impl.cc @@ -26,13 +26,18 @@ namespace Envoy { namespace Event { DispatcherImpl::DispatcherImpl() - : DispatcherImpl(Buffer::WatermarkFactoryPtr{new Buffer::WatermarkBufferFactory}) {} + : DispatcherImpl(Buffer::WatermarkFactoryPtr{new Buffer::WatermarkBufferFactory}) { + // The dispatcher won't work as expected if libevent hasn't been configured to use threads. + RELEASE_ASSERT(Libevent::Global::initialized()); +} DispatcherImpl::DispatcherImpl(Buffer::WatermarkFactoryPtr&& factory) : buffer_factory_(std::move(factory)), base_(event_base_new()), deferred_delete_timer_(createTimer([this]() -> void { clearDeferredDeleteList(); })), post_timer_(createTimer([this]() -> void { runPostCallbacks(); })), - current_to_delete_(&to_delete_1_) {} + current_to_delete_(&to_delete_1_) { + RELEASE_ASSERT(Libevent::Global::initialized()); +} DispatcherImpl::~DispatcherImpl() {} diff --git a/source/common/event/libevent.cc b/source/common/event/libevent.cc index fb206ce8bee80..a81fbeb82adc2 100644 --- a/source/common/event/libevent.cc +++ b/source/common/event/libevent.cc @@ -10,11 +10,14 @@ namespace Envoy { namespace Event { namespace Libevent { +bool Global::initialized_ = false; + void Global::initialize() { evthread_use_pthreads(); // Ignore SIGPIPE and allow errors to propagate through error codes. signal(SIGPIPE, SIG_IGN); + initialized_ = true; } } // namespace Libevent diff --git a/source/common/event/libevent.h b/source/common/event/libevent.h index 6e7d68216ee90..a07a8816e9e2d 100644 --- a/source/common/event/libevent.h +++ b/source/common/event/libevent.h @@ -31,10 +31,16 @@ namespace Libevent { */ class Global { public: + static bool initialized() { return initialized_; } + /** * Initialize the library globally. */ static void initialize(); + +private: + // True if initialized() has been called. + static bool initialized_; }; typedef CSmartPtr BasePtr; diff --git a/test/integration/integration.cc b/test/integration/integration.cc index 7499c0b2112ae..53f6bad87d92a 100644 --- a/test/integration/integration.cc +++ b/test/integration/integration.cc @@ -15,6 +15,7 @@ #include "common/buffer/buffer_impl.h" #include "common/common/assert.h" #include "common/event/dispatcher_impl.h" +#include "common/event/libevent.h" #include "common/network/connection_impl.h" #include "common/network/utility.h" #include "common/upstream/upstream_impl.h" @@ -199,6 +200,7 @@ Network::ClientConnectionPtr BaseIntegrationTest::makeClientConnection(uint32_t void BaseIntegrationTest::initialize() { RELEASE_ASSERT(!initialized_); + RELEASE_ASSERT(Event::Libevent::Global::initialized()); initialized_ = true; createUpstreams();