Skip to content

Commit

Permalink
Fix a race condition bug of http::Service. #28
Browse files Browse the repository at this point in the history
  • Loading branch information
zieckey committed Apr 11, 2017
1 parent de046aa commit 31f38aa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion evpp/event_loop_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void EventLoopThread::Run(const Functor& pre, const Functor& post) {
if (post) {
post();
}
event_loop_.reset(); // Make sure construct, initialize and destruct in the same thread
assert(event_loop_->IsStopped());
LOG_INFO << "this=" << this << " EventLoopThread stopped";
status_ = kStopped;
}
Expand Down
2 changes: 1 addition & 1 deletion evpp/http/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void Server::Stop(bool wait_thread_exit /*= false*/) {
LOG_INFO << "this=" << this << " http server is stopping";

// First we stop all the listening threads
// And then after listening threads have stopped,
// And then after all listening threads have been stopped,
// Server::OnListenThreadExited will be invoked automatically
// in which we will stop the working thread pool
for (auto& lt : listen_threads_) {
Expand Down
10 changes: 7 additions & 3 deletions evpp/http/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Service::Service(EventLoop* l)

Service::~Service() {
assert(!evhttp_);
assert(!listen_loop_);
assert(!evhttp_bound_socket_);
}

Expand Down Expand Up @@ -50,7 +49,6 @@ void Service::Stop() {
evhttp_bound_socket_ = nullptr;
}

listen_loop_ = nullptr;
callbacks_.clear();
default_callback_ = HTTPRequestCallback();
LOG_TRACE << "this=" << this << " http service stopped";
Expand Down Expand Up @@ -163,7 +161,13 @@ void Service::SendReply(struct evhttp_request* req, const std::string& response_
auto f = [this, response]() {
// In the main HTTP listening thread
assert(listen_loop_->IsInLoopThread());
LOG_TRACE << "this=" << this << " send reply in listening thread";
LOG_TRACE << "this=" << this << " send reply in listening thread. evhttp_=" << evhttp_;

// At this moment, this Service maybe already stopped.
if (!evhttp_) {
LOG_WARN << "this=" << this << " Service has been stopped.";
return;
}

if (!response->buffer) {
evhttp_send_reply(response->req, HTTP_NOTFOUND, "Not Found", nullptr);
Expand Down

0 comments on commit 31f38aa

Please sign in to comment.