Skip to content

Commit 617df57

Browse files
committed
fix data race
1 parent a0879d8 commit 617df57

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

include/ylt/standalone/cinatra/coro_http_client.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
14761476
struct socket_t {
14771477
asio::ip::tcp::socket impl_;
14781478
std::atomic<bool> has_closed_ = true;
1479-
bool is_timeout_ = false;
1479+
std::atomic<bool> is_timeout_ = false;
14801480
asio::streambuf head_buf_;
14811481
asio::streambuf chunked_buf_;
14821482
#ifdef CINATRA_ENABLE_SSL

include/ylt/standalone/cinatra/coro_http_server.hpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class coro_http_server {
8888

8989
if (!errc_) {
9090
if (out_ctx_ == nullptr) {
91+
std::lock_guard lock(thd_mtx_);
9192
thd_ = std::thread([this] {
9293
pool_->run();
9394
});
@@ -112,13 +113,14 @@ class coro_http_server {
112113

113114
// only call once, not thread safe.
114115
void stop() {
115-
if (out_ctx_ == nullptr && !thd_.joinable()) {
116-
return;
116+
{
117+
std::lock_guard lock(thd_mtx_);
118+
if (out_ctx_ == nullptr && !thd_.joinable()) {
119+
return;
120+
}
117121
}
118122

119123
stop_timer_ = true;
120-
std::error_code ec;
121-
check_timer_.cancel(ec);
122124

123125
close_acceptor();
124126

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

138140
CINATRA_LOG_INFO << "server's thread-pool finished.";
139-
thd_.join();
141+
{
142+
std::lock_guard lock(thd_mtx_);
143+
thd_.join();
144+
}
145+
140146
CINATRA_LOG_INFO << "stop coro_http_server ok";
141147
}
142148
else {
@@ -977,6 +983,7 @@ class coro_http_server {
977983
std::error_code errc_ = {};
978984
asio::ip::tcp::acceptor acceptor_;
979985
std::thread thd_;
986+
std::mutex thd_mtx_;
980987
std::promise<void> acceptor_close_waiter_;
981988
bool no_delay_ = true;
982989

include/ylt/standalone/cinatra/session_manager.hpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ class session_manager {
6060
}
6161

6262
void start_check_session_timer() {
63-
check_session_timer_.expires_after(check_session_duration_);
64-
check_session_timer_.async_wait([this](auto ec) {
63+
std::weak_ptr<asio::steady_timer> timer = check_session_timer_;
64+
check_session_timer_->expires_after(check_session_duration_);
65+
check_session_timer_->async_wait([this, timer](auto ec) {
66+
auto ptr = timer.lock();
67+
if (ptr == nullptr) {
68+
return;
69+
}
70+
6571
if (ec || stop_timer_) {
6672
return;
6773
}
@@ -76,16 +82,12 @@ class session_manager {
7682
start_check_session_timer();
7783
}
7884

79-
void stop_timer() {
80-
stop_timer_ = true;
81-
std::error_code ec;
82-
check_session_timer_.cancel(ec);
83-
}
85+
void stop_timer() { stop_timer_ = true; }
8486

8587
private:
8688
session_manager()
87-
: check_session_timer_(
88-
coro_io::get_global_executor()->get_asio_executor()) {
89+
: check_session_timer_(std::make_shared<asio::steady_timer>(
90+
coro_io::get_global_executor()->get_asio_executor())) {
8991
start_check_session_timer();
9092
};
9193
session_manager(const session_manager &) = delete;
@@ -98,9 +100,9 @@ class session_manager {
98100
// session_timeout_ should be no less than 0
99101
std::size_t session_timeout_ = 86400;
100102
std::atomic<bool> stop_timer_ = false;
101-
asio::steady_timer check_session_timer_;
102-
std::chrono::steady_clock::duration check_session_duration_ =
103-
std::chrono::seconds(15);
103+
std::shared_ptr<asio::steady_timer> check_session_timer_;
104+
std::atomic<std::chrono::steady_clock::duration> check_session_duration_ = {
105+
std::chrono::seconds(15)};
104106
};
105107

106108
} // namespace cinatra

src/coro_http/tests/test_cinatra.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,12 +2706,12 @@ TEST_CASE("test coro http redirect request") {
27062706
if (result.status != 404 && !result.net_err) {
27072707
CHECK(!result.net_err);
27082708
if (result.status < 500)
2709-
CHECK(result.status == 302);
2709+
CHECK((result.status == 302 || result.status == 301));
27102710

27112711
if (client.is_redirect(result)) {
27122712
std::string redirect_uri = client.get_redirect_uri();
27132713
result = async_simple::coro::syncAwait(client.async_get(redirect_uri));
2714-
if (result.status < 400)
2714+
if (result.status < 300)
27152715
CHECK(result.status == 200);
27162716
}
27172717

src/coro_http/tests/test_cinatra_websocket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ TEST_CASE("test read write in different threads") {
330330
}
331331
};
332332

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

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

0 commit comments

Comments
 (0)