Skip to content

Commit

Permalink
fix data race
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Feb 5, 2025
1 parent a0879d8 commit 617df57
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion include/ylt/standalone/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
struct socket_t {
asio::ip::tcp::socket impl_;
std::atomic<bool> has_closed_ = true;
bool is_timeout_ = false;
std::atomic<bool> is_timeout_ = false;
asio::streambuf head_buf_;
asio::streambuf chunked_buf_;
#ifdef CINATRA_ENABLE_SSL
Expand Down
17 changes: 12 additions & 5 deletions include/ylt/standalone/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class coro_http_server {

if (!errc_) {
if (out_ctx_ == nullptr) {
std::lock_guard lock(thd_mtx_);
thd_ = std::thread([this] {
pool_->run();
});
Expand All @@ -112,13 +113,14 @@ class coro_http_server {

// only call once, not thread safe.
void stop() {
if (out_ctx_ == nullptr && !thd_.joinable()) {
return;
{
std::lock_guard lock(thd_mtx_);
if (out_ctx_ == nullptr && !thd_.joinable()) {
return;
}
}

stop_timer_ = true;
std::error_code ec;
check_timer_.cancel(ec);

close_acceptor();

Expand All @@ -136,7 +138,11 @@ class coro_http_server {
pool_->stop();

CINATRA_LOG_INFO << "server's thread-pool finished.";
thd_.join();
{
std::lock_guard lock(thd_mtx_);
thd_.join();
}

CINATRA_LOG_INFO << "stop coro_http_server ok";
}
else {
Expand Down Expand Up @@ -977,6 +983,7 @@ class coro_http_server {
std::error_code errc_ = {};
asio::ip::tcp::acceptor acceptor_;
std::thread thd_;
std::mutex thd_mtx_;
std::promise<void> acceptor_close_waiter_;
bool no_delay_ = true;

Expand Down
26 changes: 14 additions & 12 deletions include/ylt/standalone/cinatra/session_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ class session_manager {
}

void start_check_session_timer() {
check_session_timer_.expires_after(check_session_duration_);
check_session_timer_.async_wait([this](auto ec) {
std::weak_ptr<asio::steady_timer> timer = check_session_timer_;
check_session_timer_->expires_after(check_session_duration_);
check_session_timer_->async_wait([this, timer](auto ec) {
auto ptr = timer.lock();
if (ptr == nullptr) {
return;
}

if (ec || stop_timer_) {
return;
}
Expand All @@ -76,16 +82,12 @@ class session_manager {
start_check_session_timer();
}

void stop_timer() {
stop_timer_ = true;
std::error_code ec;
check_session_timer_.cancel(ec);
}
void stop_timer() { stop_timer_ = true; }

private:
session_manager()
: check_session_timer_(
coro_io::get_global_executor()->get_asio_executor()) {
: check_session_timer_(std::make_shared<asio::steady_timer>(
coro_io::get_global_executor()->get_asio_executor())) {
start_check_session_timer();
};
session_manager(const session_manager &) = delete;
Expand All @@ -98,9 +100,9 @@ class session_manager {
// session_timeout_ should be no less than 0
std::size_t session_timeout_ = 86400;
std::atomic<bool> stop_timer_ = false;
asio::steady_timer check_session_timer_;
std::chrono::steady_clock::duration check_session_duration_ =
std::chrono::seconds(15);
std::shared_ptr<asio::steady_timer> check_session_timer_;
std::atomic<std::chrono::steady_clock::duration> check_session_duration_ = {
std::chrono::seconds(15)};
};

} // namespace cinatra
4 changes: 2 additions & 2 deletions src/coro_http/tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2706,12 +2706,12 @@ TEST_CASE("test coro http redirect request") {
if (result.status != 404 && !result.net_err) {
CHECK(!result.net_err);
if (result.status < 500)
CHECK(result.status == 302);
CHECK((result.status == 302 || result.status == 301));

if (client.is_redirect(result)) {
std::string redirect_uri = client.get_redirect_uri();
result = async_simple::coro::syncAwait(client.async_get(redirect_uri));
if (result.status < 400)
if (result.status < 300)
CHECK(result.status == 200);
}

Expand Down
2 changes: 1 addition & 1 deletion src/coro_http/tests/test_cinatra_websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ TEST_CASE("test read write in different threads") {
}
};

async_simple::coro::syncAwait(lazy());
async_simple::coro::syncAwait(lazy().via(&client->get_executor()));

promise.get_future().wait_for(std::chrono::seconds(2));

Expand Down

0 comments on commit 617df57

Please sign in to comment.