From: Dave Watson Date: Fri, 3 Apr 2015 20:07:13 +0000 (-0700) Subject: shared ptr vector sockets X-Git-Tag: v0.35.0~15 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=bd0960873af1941bc31d19a1a80a476fb01cfb31;p=folly.git shared ptr vector sockets Summary: promote the sockets vector to a shared_ptr, since both ServerWorkerPool and ServerBootstrap use it. Otherwise there are destruction order issues between ServerBootstrap and any IOThreadPoolExecutor you use Test Plan: Saw use after free in D1942242, gone after this. Reviewed By: yfeldblum@fb.com Subscribers: chalfant, doug, fugalh, folly-diffs@, jsedgwick, yfeldblum FB internal diff: D1947553 Signature: t1:1947553:1427484417:5b78f5c9c70d244d3f52a6f71b6d1fab7b29d106 --- diff --git a/folly/wangle/bootstrap/ServerBootstrap-inl.h b/folly/wangle/bootstrap/ServerBootstrap-inl.h index a6ffd268..ecc13721 100644 --- a/folly/wangle/bootstrap/ServerBootstrap-inl.h +++ b/folly/wangle/bootstrap/ServerBootstrap-inl.h @@ -140,7 +140,7 @@ class ServerWorkerPool : public folly::wangle::ThreadPoolExecutor::Observer { explicit ServerWorkerPool( std::shared_ptr acceptorFactory, folly::wangle::IOThreadPoolExecutor* exec, - std::vector>* sockets, + std::shared_ptr>> sockets, std::shared_ptr socketFactory) : acceptorFactory_(acceptorFactory) , exec_(exec) @@ -170,7 +170,7 @@ class ServerWorkerPool : public folly::wangle::ThreadPoolExecutor::Observer { std::shared_ptr> workers_; std::shared_ptr acceptorFactory_; folly::wangle::IOThreadPoolExecutor* exec_{nullptr}; - std::vector>* sockets_; + std::shared_ptr>> sockets_; std::shared_ptr socketFactory_; }; diff --git a/folly/wangle/bootstrap/ServerBootstrap.cpp b/folly/wangle/bootstrap/ServerBootstrap.cpp index 3f5592a6..e59ed657 100644 --- a/folly/wangle/bootstrap/ServerBootstrap.cpp +++ b/folly/wangle/bootstrap/ServerBootstrap.cpp @@ -39,7 +39,7 @@ void ServerWorkerPool::threadStopped( auto worker = workers_.find(h); CHECK(worker != workers_.end()); - for (auto& socket : *sockets_) { + for (auto socket : *sockets_) { socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait( [&]() { socketFactory_->removeAcceptCB( diff --git a/folly/wangle/bootstrap/ServerBootstrap.h b/folly/wangle/bootstrap/ServerBootstrap.h index 98890b43..3520b0f5 100644 --- a/folly/wangle/bootstrap/ServerBootstrap.h +++ b/folly/wangle/bootstrap/ServerBootstrap.h @@ -132,13 +132,13 @@ class ServerBootstrap { if (acceptorFactory_) { workerFactory_ = std::make_shared( - acceptorFactory_, io_group.get(), &sockets_, socketFactory_); + acceptorFactory_, io_group.get(), sockets_, socketFactory_); } else { workerFactory_ = std::make_shared( std::make_shared>( childPipelineFactory_, pipeline_), - io_group.get(), &sockets_, socketFactory_); + io_group.get(), sockets_, socketFactory_); } io_group->addObserver(workerFactory_); @@ -183,7 +183,7 @@ class ServerBootstrap { }); }); - sockets_.push_back(socket); + sockets_->push_back(socket); } void bind(folly::SocketAddress& address) { @@ -268,7 +268,7 @@ class ServerBootstrap { }); }); - sockets_.push_back(socket); + sockets_->push_back(socket); } } @@ -276,13 +276,16 @@ class ServerBootstrap { * Stop listening on all sockets. */ void stop() { - for (auto socket : sockets_) { - socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait( - [&]() mutable { - socketFactory_->stopSocket(socket); - }); + // sockets_ may be null if ServerBootstrap has been std::move'd + if (sockets_) { + for (auto socket : *sockets_) { + socket->getEventBase()->runImmediatelyOrRunInEventBaseThreadAndWait( + [&]() mutable { + socketFactory_->stopSocket(socket); + }); + } + sockets_->clear(); } - sockets_.clear(); } void join() { @@ -299,7 +302,7 @@ class ServerBootstrap { */ const std::vector>& getSockets() const { - return sockets_; + return *sockets_; } std::shared_ptr getIOGroup() const { @@ -318,7 +321,8 @@ class ServerBootstrap { std::shared_ptr io_group_; std::shared_ptr workerFactory_; - std::vector> sockets_; + std::shared_ptr>> sockets_{ + std::make_shared>>()}; std::shared_ptr acceptorFactory_; std::shared_ptr> childPipelineFactory_;