Corvusoft / restbed

Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
http://www.corvusoft.co.uk
Other
1.93k stars 377 forks source link

Service::m_uptime must be thread-safe #499

Closed bansan85 closed 2 years ago

bansan85 commented 2 years ago

In service, the field m_uptime should be thread-safe.

I started a service in a thread. In another thread I checked if service is_up.

I may have a data race : when service is up, the service write in m_uptime and the other thread may read m_uptime via is_up.

The solution is not perfect. In get_uptime, the service could be down between is_down and the read of the value but it's better than nothing and avoid the use of mutex.

ben-crowhurst commented 2 years ago

Could you provide some addtional detail regarding the error that is caused?

bansan85 commented 2 years ago

Considers these 3 lines:

  restbed::Service service_;

  std::thread server([&service_]() { service_.start(settings); });

  while (!service_.is_up()){}

The function is_up is used to know when the service has started.

The data race occurs when the function start ends :

It's not really a critical bug but a thread sanitizer will complain about this undefined behavior.

ben-crowhurst commented 2 years ago

Thanks for catching this issue. Sorry for the delay.