Skip to content

Fix a race condition bug of http::Service #28

@zieckey

Description

@zieckey
void Service::Stop() {
    LOG_TRACE << "this=" << this << " http service is stopping";
    assert(listen_loop_->IsInLoopThread());

    if (evhttp_) {
        evhttp_free(evhttp_);
        evhttp_ = nullptr;
        evhttp_bound_socket_ = nullptr;
    }

    listen_loop_ = nullptr;
    callbacks_.clear();
    default_callback_ = HTTPRequestCallback();
    LOG_TRACE << "this=" << this << " http service stopped";
}

void Service::SendReply(struct evhttp_request* req, const std::string& response_data) {
    ...

    if (listen_loop_->IsRunning()) {
        LOG_INFO << "this=" << this << " dispatch this SendReply to listening thread";
        listen_loop_->RunInLoop(f);
    } else {
        LOG_WARN << "this=" << this << " listening thread is going to stop. we discards this request.";
        // TODO do we need do some resource recycling about the evhttp_request?
    }
}

The above code, Service::SendReply in work thread will use listen_loop_ at the time that this object listen_loop_ is be set to nullptr in Service::Stop()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions