Skip to content

Commit d2f51ea

Browse files
committed
fix http server callback
1 parent f7b7c73 commit d2f51ea

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

include/ylt/standalone/cinatra/coro_http_server.hpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class coro_http_server {
121121

122122
// close current connections.
123123
{
124-
std::scoped_lock lock(conn_mtx_);
124+
std::scoped_lock lock(*conn_mtx_);
125125
for (auto &conn : connections_) {
126126
conn.second->close(false);
127127
}
@@ -595,7 +595,7 @@ class coro_http_server {
595595
}
596596

597597
size_t connection_count() {
598-
std::scoped_lock lock(conn_mtx_);
598+
std::scoped_lock lock(*conn_mtx_);
599599
return connections_.size();
600600
}
601601

@@ -708,17 +708,20 @@ class coro_http_server {
708708
conn->init_ssl(cert_file_, key_file_, passwd_);
709709
}
710710
#endif
711-
711+
std::weak_ptr<std::mutex> weak(conn_mtx_);
712712
conn->set_quit_callback(
713-
[this](const uint64_t &id) {
714-
std::scoped_lock lock(conn_mtx_);
715-
if (!connections_.empty())
716-
connections_.erase(id);
713+
[this, weak](const uint64_t &id) {
714+
auto mtx = weak.lock();
715+
if (mtx) {
716+
std::scoped_lock lock(*mtx);
717+
if (!connections_.empty())
718+
connections_.erase(id);
719+
}
717720
},
718721
conn_id);
719722

720723
{
721-
std::scoped_lock lock(conn_mtx_);
724+
std::scoped_lock lock(*conn_mtx_);
722725
connections_.emplace(conn_id, conn);
723726
}
724727

@@ -758,7 +761,7 @@ class coro_http_server {
758761
std::unordered_map<uint64_t, std::shared_ptr<coro_http_connection>> conns;
759762

760763
{
761-
std::scoped_lock lock(conn_mtx_);
764+
std::scoped_lock lock(*conn_mtx_);
762765
for (auto it = connections_.begin();
763766
it != connections_.end();) // no "++"!
764767
{
@@ -966,7 +969,7 @@ class coro_http_server {
966969
uint64_t conn_id_ = 0;
967970
std::unordered_map<uint64_t, std::shared_ptr<coro_http_connection>>
968971
connections_;
969-
std::mutex conn_mtx_;
972+
std::shared_ptr<std::mutex> conn_mtx_ = std::make_shared<std::mutex>();
970973
std::chrono::steady_clock::duration check_duration_ =
971974
std::chrono::seconds(15);
972975
std::chrono::steady_clock::duration timeout_duration_{};

0 commit comments

Comments
 (0)