Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Draft Mutexing. Testing Now Needed. #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

avikbmohan
Copy link
Collaborator

No description provided.

Copy link
Contributor

@richardmin richardmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to review comments.
Issues regarding code locking

@@ -21,10 +21,11 @@ RequestHandler::Status StatusHandler::HandleRequest(const Request& request,

ServerStatus::Status status = status_->GetStatus();


status.stsMx->lock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you locking the status here? You lock it and then don't use the resource.

@@ -21,10 +21,11 @@ RequestHandler::Status StatusHandler::HandleRequest(const Request& request,

ServerStatus::Status status = status_->GetStatus();


status.stsMx->lock();
response->SetStatus(Response::OK);
response->AddHeader("Content-Type", http::mime_type::ContentTypeAsString(http::mime_type::CONTENT_TYPE_TEXT_HTML));
response->SetBody(StatusToHtml(status));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, you should lock it right above here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I see that now, moved

@@ -33,15 +34,17 @@ RequestHandler::Status StatusHandler::InitStatus(ServerStatus* status) {
if (status == nullptr) {
return RequestHandler::MISCONFIGURED_HANDLER;
}
status->GetStatus().stsMx->lock();
status_ = status;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call on the copy constructor is not safe -- you're copying the mutex, for example

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the best way to remedy this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this was my mistake

@@ -92,8 +92,30 @@ void Session::do_read() {
status_->LogIncomingRequest(req->uri(), 400);
}

do_write(response_string);
delete response;
if(RequestStatus == RequestHandler::BAD_REQUEST) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this code relate to the code above it?

Also tabbing is wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a copy paste error, fixed.

@@ -134,5 +134,6 @@ bool WebServer::run_server() {
printf("Exception %s\n", e.what());
return false;
}
status_.GetStatus().destroyMutex();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like this should be part of the default constructor

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean destructor? The problem with that is that it was being called multiple times on one instance, producing segfaults. Likely related to copy error above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check if a object is valid (i.e. it still exists) before destructing it.
Either way the struct should never be copied by value

std::map<std::string, int> RequestCountByURL_;
std::map<int, int> ResponseCountByCode_;
std::map<std::string, std::string> RequestHandlers_;

std::mutex* stsMx;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a underscore aftewards to indicate data internals.

@richardmin
Copy link
Contributor

@avikbmohan when are you getting back to this...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants