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

Fix webserver thread safety #330

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

Conversation

FlorianChevassu
Copy link

Identify the Bug

#329

Description of the Change

This pull request introduces the usage of locks in the werbserver class in order to protect member variables from data races.

Alternate Designs

N/A

Possible Drawbacks

This might slightly increase response time, but should not be noticeable.

Verification Process

N/A

Release Notes

Fixed an issue where multiple threads did access webserver member variables concurrently, leading to data races.

std::map<details::http_endpoint, http_resource*> registered_resources;
std::map<std::string, http_resource*> registered_resources_str;

std::shared_mutex bans_and_allowances_mutex;
Copy link
Owner

Choose a reason for hiding this comment

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

It might be worth splitting this into two, given the writing use-cases are isolated

Copy link
Author

Choose a reason for hiding this comment

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

done

@@ -402,6 +409,7 @@ MHD_Result policy_callback(void *cls, const struct sockaddr* addr, socklen_t add

if (!(static_cast<webserver*>(cls))->ban_system_enabled) return MHD_YES;

std::shared_lock bans_and_allowances_lock((static_cast<webserver*>(cls))->bans_and_allowances_mutex);
Copy link
Owner

Choose a reason for hiding this comment

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

We can split the if statement here so that we always check only one of the two based on the chosen policy

Copy link
Author

Choose a reason for hiding this comment

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

In both ACCEPT and REJECT cases, we access bans and allowances, so I don't see how we could only lock one of the mutexes.

I added a commit simplifying the code, with no intended functional change.

Copy link
Owner

Choose a reason for hiding this comment

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

[MINOR] I think the cleanest way would be to have two private methods (is_allowed and is_banned) that use the locks internally and just call into those methods from within here. That should localize the locks to the maximum extent possible.

@etr
Copy link
Owner

etr commented Aug 1, 2023

Overall, it looks good - just a few minor comments

@etr
Copy link
Owner

etr commented Aug 17, 2023

It looks like the PR is failing at compiling

@FlorianChevassu
Copy link
Author

It looks like the PR is failing at compiling

Sorry about the delay. Should be fixed now.

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.

3 participants