From fd6de9bc0588655fc16baebab8178a7e9fb6e4fb Mon Sep 17 00:00:00 2001 From: The-EDev Date: Sat, 6 Nov 2021 06:06:18 +0300 Subject: [PATCH 1/6] Added functionality to close websocket connections before the app is terminated. This is incomplete and needs more work. --- examples/websocket/templates/ws.html | 4 +- include/crow/app.h | 6 ++- include/crow/http_server.h | 50 ++++++++++++++++--------- include/crow/routing.h | 10 ++--- include/crow/websocket.h | 56 +++++++++++++++++++--------- 5 files changed, 83 insertions(+), 43 deletions(-) diff --git a/examples/websocket/templates/ws.html b/examples/websocket/templates/ws.html index 2d38fdfce..6465d9f06 100644 --- a/examples/websocket/templates/ws.html +++ b/examples/websocket/templates/ws.html @@ -19,8 +19,8 @@ sock.onopen = ()=>{ sock.onerror = (e)=>{ console.log('error',e) } -sock.onclose = ()=>{ - console.log('close') +sock.onclose = (e)=>{ + console.log('close', e) } sock.onmessage = (e)=>{ $("#log").val( diff --git a/include/crow/app.h b/include/crow/app.h index 083cca618..4660274f2 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -62,6 +62,9 @@ namespace crow { } + + std::atomic websocket_count{0}; + ///Process an Upgrade request /// @@ -69,7 +72,7 @@ namespace crow template void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) { - router_.handle_upgrade(req, res, adaptor); + router_.handle_upgrade(req, res, adaptor, websocket_count); } ///Process the request and generate a response for it @@ -289,7 +292,6 @@ namespace crow { server_ = std::move(std::unique_ptr(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, nullptr))); server_->set_tick_function(tick_interval_, tick_function_); - server_->signal_clear(); for (auto snum : signals_) { server_->signal_add(snum); diff --git a/include/crow/http_server.h b/include/crow/http_server.h index e6efa6fb0..87b96ecfb 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -29,7 +29,7 @@ namespace crow public: Server(Handler* handler, std::string bindaddr, uint16_t port, std::string server_name = std::string("Crow/") + VERSION, std::tuple* middlewares = nullptr, uint16_t concurrency = 1, typename Adaptor::context* adaptor_ctx = nullptr) : acceptor_(io_service_, tcp::endpoint(boost::asio::ip::address::from_string(bindaddr), port)), - signals_(io_service_, SIGINT, SIGTERM), + signals_(io_service_), tick_timer_(io_service_), handler_(handler), concurrency_(concurrency == 0 ? 1 : concurrency), @@ -169,9 +169,21 @@ namespace crow void stop() { - io_service_.stop(); + should_close_ = false; //Prevent the acceptor from taking new connections + while (handler_->websocket_count.load(std::memory_order_release) != 0) //Wait for the websockets to close properly + { + } for(auto& io_service:io_service_pool_) - io_service->stop(); + { + if (io_service != nullptr) + { + CROW_LOG_INFO << "Closing IO service " << &io_service; + io_service->stop(); //Close all io_services (and HTTP connections) + } + } + + CROW_LOG_INFO << "Closing main IO service (" << &io_service_ << ')'; + io_service_.stop(); //Close main io_service } void signal_clear() @@ -201,22 +213,25 @@ namespace crow is, handler_, server_name_, middlewares_, get_cached_date_str_pool_[roundrobin_index_], *timer_queue_pool_[roundrobin_index_], adaptor_ctx_); - acceptor_.async_accept(p->socket(), - [this, p, &is](boost::system::error_code ec) - { - if (!ec) + if (!should_close_) + { + acceptor_.async_accept(p->socket(), + [this, p, &is](boost::system::error_code ec) { - is.post([p] + if (!ec) { - p->start(); - }); - } - else - { - delete p; - } - do_accept(); - }); + is.post([p] + { + p->start(); + }); + } + else + { + delete p; + } + do_accept(); + }); + } } private: @@ -225,6 +240,7 @@ namespace crow std::vector timer_queue_pool_; std::vector> get_cached_date_str_pool_; tcp::acceptor acceptor_; + bool should_close_ = false; boost::asio::signal_set signals_; boost::asio::deadline_timer tick_timer_; diff --git a/include/crow/routing.h b/include/crow/routing.h index eca2bf5e4..8800732f5 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -49,7 +49,7 @@ namespace crow } virtual void handle(const request&, response&, const routing_params&) = 0; - virtual void handle_upgrade(const request&, response& res, SocketAdaptor&&) + virtual void handle_upgrade(const request&, response& res, SocketAdaptor&&, std::atomic&) { res = response(404); res.end(); @@ -400,9 +400,9 @@ namespace crow res.end(); } - void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor) override + void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor, std::atomic& websocket_count) override { - new crow::websocket::Connection(req, std::move(adaptor), open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); + new crow::websocket::Connection(req, std::move(adaptor), websocket_count, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); } #ifdef CROW_ENABLE_SSL void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override @@ -1397,7 +1397,7 @@ namespace crow //TODO maybe add actual_method template - void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) + void handle_upgrade(const request& req, response& res, Adaptor&& adaptor, std::atomic& websocket_count) { if (req.method >= HTTPMethod::InternalMethodCount) return; @@ -1451,7 +1451,7 @@ namespace crow // any uncaught exceptions become 500s try { - rules[rule_index]->handle_upgrade(req, res, std::move(adaptor)); + rules[rule_index]->handle_upgrade(req, res, std::move(adaptor), websocket_count); } catch(std::exception& e) { diff --git a/include/crow/websocket.h b/include/crow/websocket.h index 86120735f..0bc758e59 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include "crow/logging.h" #include "crow/socket_adaptors.h" #include "crow/http_request.h" #include "crow/TinySHA1.hpp" @@ -56,7 +57,7 @@ namespace crow // +---------------------------------------------------------------+ /// A websocket connection. - template + template class Connection : public connection { public: @@ -65,19 +66,20 @@ namespace crow /// /// Requires a request with an "Upgrade: websocket" header.
/// Automatically handles the handshake. - Connection(const crow::request& req, Adaptor&& adaptor, + Connection(const crow::request& req, Adaptor&& adaptor, std::atomic& websocket_count, std::function open_handler, std::function message_handler, std::function close_handler, std::function error_handler, std::function accept_handler) - : adaptor_(std::move(adaptor)), open_handler_(std::move(open_handler)), message_handler_(std::move(message_handler)), close_handler_(std::move(close_handler)), error_handler_(std::move(error_handler)) - , accept_handler_(std::move(accept_handler)) + : adaptor_(std::move(adaptor)), websocket_count_(websocket_count), open_handler_(std::move(open_handler)), message_handler_(std::move(message_handler)), close_handler_(std::move(close_handler)), error_handler_(std::move(error_handler)) + , accept_handler_(std::move(accept_handler)), signals_(adaptor_.get_io_service(), SIGINT, SIGTERM) { + if (!boost::iequals(req.get_header_value("upgrade"), "websocket")) { - adaptor.close(); - delete this; + adaptor.close(); + delete this; return; } @@ -85,19 +87,28 @@ namespace crow { if (!accept_handler_(req)) { - adaptor.close(); - delete this; + adaptor.close(); + delete this; return; } } + websocket_count_++; // Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== // Sec-WebSocket-Version: 13 - std::string magic = req.get_header_value("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + std::string magic = req.get_header_value("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; sha1::SHA1 s; s.processBytes(magic.data(), magic.size()); uint8_t digest[20]; - s.getDigestBytes(digest); + s.getDigestBytes(digest); + signals_.async_wait( + [&](const boost::system::error_code& e, int /*signal_number*/){ + if (!e){ + CROW_LOG_INFO << "quitting " << this; + do_not_destroy_ = true; + close("Quitter"); + } + }); start(crow::utility::base64encode((unsigned char*)digest, 20)); } @@ -307,7 +318,7 @@ namespace crow { remaining_length_ = 0; remaining_length16_ = 0; - boost::asio::async_read(adaptor_.socket(), boost::asio::buffer(&remaining_length16_, 2), + boost::asio::async_read(adaptor_.socket(), boost::asio::buffer(&remaining_length16_, 2), [this](const boost::system::error_code& ec, std::size_t #ifdef CROW_ENABLE_DEBUG bytes_transferred @@ -342,7 +353,7 @@ namespace crow break; case WebSocketReadState::Len64: { - boost::asio::async_read(adaptor_.socket(), boost::asio::buffer(&remaining_length_, 8), + boost::asio::async_read(adaptor_.socket(), boost::asio::buffer(&remaining_length_, 8), [this](const boost::system::error_code& ec, std::size_t #ifdef CROW_ENABLE_DEBUG bytes_transferred @@ -417,7 +428,7 @@ namespace crow auto to_read = static_cast(buffer_.size()); if (remaining_length_ < to_read) to_read = remaining_length_; - adaptor_.socket().async_read_some(boost::asio::buffer(buffer_, static_cast(to_read)), + adaptor_.socket().async_read_some(boost::asio::buffer(buffer_, static_cast(to_read)), [this](const boost::system::error_code& ec, std::size_t bytes_transferred) { is_reading = false; @@ -561,7 +572,7 @@ namespace crow { buffers.emplace_back(boost::asio::buffer(s)); } - boost::asio::async_write(adaptor_.socket(), buffers, + boost::asio::async_write(adaptor_.socket(), buffers, [&](const boost::system::error_code& ec, std::size_t /*bytes_transferred*/) { sending_buffers_.clear(); @@ -588,11 +599,12 @@ namespace crow if (!is_close_handler_called_) if (close_handler_) close_handler_(*this, "uncleanly"); - if (sending_buffers_.empty() && !is_reading) + websocket_count_--; + if (sending_buffers_.empty() && !is_reading && !do_not_destroy_) delete this; } - private: - Adaptor adaptor_; + private: + Adaptor adaptor_; std::vector sending_buffers_; std::vector write_buffers_; @@ -615,11 +627,21 @@ namespace crow bool pong_received_{false}; bool is_close_handler_called_{false}; + //**WARNING** + //SETTING THIS PREVENTS THE OBJECT FROM BEING DELETED, + //AND WILL ABSOLUTELY CAUSE A MEMORY LEAK!! + //ONLY USE IF THE APPLICATION IS BEING TERMINATED!! + bool do_not_destroy_{false}; + //**WARNING** + + std::atomic& websocket_count_; + std::function open_handler_; std::function message_handler_; std::function close_handler_; std::function error_handler_; std::function accept_handler_; + boost::asio::signal_set signals_; }; } } From e5fe4e6e269a6a3314e1c6c2c22fbcb8a59edff3 Mon Sep 17 00:00:00 2001 From: The-EDev Date: Sun, 15 May 2022 01:15:39 +0300 Subject: [PATCH 2/6] Used App reference to get websocket count and signals. Also fixed issue where enabling SSL prevented compilation. --- include/crow/app.h | 7 ++++++- include/crow/routing.h | 10 +++++----- include/crow/websocket.h | 32 ++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/include/crow/app.h b/include/crow/app.h index 1b3ce1be7..c652fbf38 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -69,7 +69,7 @@ namespace crow template void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) { - router_.handle_upgrade(req, res, adaptor, websocket_count); + router_.handle_upgrade(req, res, adaptor); } /// Process the request and generate a response for it @@ -118,6 +118,11 @@ namespace crow return *this; } + std::vector signals() + { + return signals_; + } + /// Set the port that Crow will handle requests on self_t& port(std::uint16_t port) { diff --git a/include/crow/routing.h b/include/crow/routing.h index 2a69bf03b..0122b4b9e 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -46,7 +46,7 @@ namespace crow } virtual void handle(request&, response&, const routing_params&) = 0; - virtual void handle_upgrade(const request&, response& res, SocketAdaptor&&, std::atomic&) + virtual void handle_upgrade(const request&, response& res, SocketAdaptor&&) { res = response(404); res.end(); @@ -388,9 +388,9 @@ namespace crow res.end(); } - void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor, std::atomic& websocket_count) override + void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor) override { - new crow::websocket::Connection(req, std::move(adaptor), websocket_count, app_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); + new crow::websocket::Connection(req, std::move(adaptor), app_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); } #ifdef CROW_ENABLE_SSL void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override @@ -1303,7 +1303,7 @@ namespace crow // TODO maybe add actual_method template - void handle_upgrade(const request& req, response& res, Adaptor&& adaptor, std::atomic& websocket_count) + void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) { if (req.method >= HTTPMethod::InternalMethodCount) return; @@ -1357,7 +1357,7 @@ namespace crow // any uncaught exceptions become 500s try { - rules[rule_index]->handle_upgrade(req, res, std::move(adaptor), websocket_count); + rules[rule_index]->handle_upgrade(req, res, std::move(adaptor)); } catch (std::exception& e) { diff --git a/include/crow/websocket.h b/include/crow/websocket.h index 90f3bc851..54dd7d953 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -71,7 +71,7 @@ namespace crow /// /// Requires a request with an "Upgrade: websocket" header.
/// Automatically handles the handshake. - Connection(const crow::request& req, Adaptor&& adaptor, Handler* handler, std::atomic& websocket_count, + Connection(const crow::request& req, Adaptor&& adaptor, Handler* handler, std::function open_handler, std::function message_handler, std::function close_handler, @@ -79,12 +79,13 @@ namespace crow std::function accept_handler): adaptor_(std::move(adaptor)), handler_(handler), - websocket_count_(websocket_count), + websocket_count_(handler_->websocket_count), open_handler_(std::move(open_handler)), message_handler_(std::move(message_handler)), close_handler_(std::move(close_handler)), error_handler_(std::move(error_handler)), - accept_handler_(std::move(accept_handler)) + accept_handler_(std::move(accept_handler)), + signals_(adaptor_.get_io_service()) { if (!boost::iequals(req.get_header_value("upgrade"), "websocket")) { @@ -103,6 +104,11 @@ namespace crow } } + + signals_.clear(); + for (auto snum: handler_->signals()) + signals_.add(snum); + // Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== // Sec-WebSocket-Version: 13 std::string magic = req.get_header_value("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; @@ -110,14 +116,15 @@ namespace crow s.processBytes(magic.data(), magic.size()); uint8_t digest[20]; s.getDigestBytes(digest); - signals_.async_wait( - [&](const boost::system::error_code& e, int /*signal_number*/){ - if (!e){ - CROW_LOG_INFO << "quitting " << this; - do_not_destroy_ = true; - close("Quitter"); - } - }); + + signals_.async_wait( + [&](const boost::system::error_code& e, int /*signal_number*/){ + if (!e){ + CROW_LOG_INFO << "quitting " << this; + do_not_destroy_ = true; + close("Quitter"); + } + }); start(crow::utility::base64encode((unsigned char*)digest, 20)); } @@ -658,12 +665,13 @@ namespace crow // **WARNING** std::atomic& websocket_count_; - + std::function open_handler_; std::function message_handler_; std::function close_handler_; std::function error_handler_; std::function accept_handler_; + boost::asio::signal_set signals_; }; } // namespace websocket } // namespace crow From 333b49bf045daf841c4936ac4585ba593de54375 Mon Sep 17 00:00:00 2001 From: The-EDev Date: Wed, 18 May 2022 12:28:51 +0300 Subject: [PATCH 3/6] Replaced `do_not_destroy_` with `adaptor_.shutdown_readwrite()` + formatting --- include/crow/http_server.h | 8 ++++---- include/crow/websocket.h | 23 ++++++++++------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/include/crow/http_server.h b/include/crow/http_server.h index d0ace3a5e..a7002d778 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -161,16 +161,16 @@ namespace crow void stop() { - should_close_ = false; //Prevent the acceptor from taking new connections + should_close_ = false; //Prevent the acceptor from taking new connections while (handler_->websocket_count.load(std::memory_order_release) != 0) //Wait for the websockets to close properly { } - for(auto& io_service : io_service_pool_) + for (auto& io_service : io_service_pool_) { if (io_service != nullptr) { - CROW_LOG_INFO << "Closing IO service " << &io_service; - io_service->stop(); //Close all io_services (and HTTP connections) + CROW_LOG_INFO << "Closing IO service " << &io_service; + io_service->stop(); //Close all io_services (and HTTP connections) } } diff --git a/include/crow/websocket.h b/include/crow/websocket.h index 54dd7d953..8d53c714e 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -120,9 +120,8 @@ namespace crow signals_.async_wait( [&](const boost::system::error_code& e, int /*signal_number*/){ if (!e){ - CROW_LOG_INFO << "quitting " << this; - do_not_destroy_ = true; - close("Quitter"); + CROW_LOG_INFO << "Quitting Websocket: " << this; + close("Server Application Terminated"); } }); start(crow::utility::base64encode((unsigned char*)digest, 20)); @@ -308,6 +307,7 @@ namespace crow has_mask_ = false; #else close_connection_ = true; + adaptor_.shutdown_readwrite(); adaptor_.close(); if (error_handler_) error_handler_(*this); @@ -333,6 +333,7 @@ namespace crow else { close_connection_ = true; + adaptor_.shutdown_readwrite(); adaptor_.close(); if (error_handler_) error_handler_(*this); @@ -370,6 +371,7 @@ namespace crow else { close_connection_ = true; + adaptor_.shutdown_readwrite(); adaptor_.close(); if (error_handler_) error_handler_(*this); @@ -404,6 +406,7 @@ namespace crow else { close_connection_ = true; + adaptor_.shutdown_readwrite(); adaptor_.close(); if (error_handler_) error_handler_(*this); @@ -440,6 +443,7 @@ namespace crow close_connection_ = true; if (error_handler_) error_handler_(*this); + adaptor_.shutdown_readwrite(); adaptor_.close(); } }); @@ -478,6 +482,7 @@ namespace crow close_connection_ = true; if (error_handler_) error_handler_(*this); + adaptor_.shutdown_readwrite(); adaptor_.close(); } }); @@ -557,6 +562,7 @@ namespace crow } else { + adaptor_.shutdown_readwrite(); adaptor_.close(); close_connection_ = true; if (!is_close_handler_called_) @@ -655,16 +661,7 @@ namespace crow bool error_occured_{false}; bool pong_received_{false}; bool is_close_handler_called_{false}; - - - // **WARNING** - // SETTING THIS PREVENTS THE OBJECT FROM BEING DELETED, - // AND WILL ABSOLUTELY CAUSE A MEMORY LEAK!! - // ONLY USE IF THE APPLICATION IS BEING TERMINATED!! - bool do_not_destroy_{false}; - // **WARNING** - - std::atomic& websocket_count_; + std::atomic& websocket_count_; std::function open_handler_; std::function message_handler_; From 4128e5c27d486e20cdc419f7c44d7a018ede16b6 Mon Sep 17 00:00:00 2001 From: The-EDev Date: Wed, 18 May 2022 12:40:11 +0300 Subject: [PATCH 4/6] further formatting --- include/crow/websocket.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/include/crow/websocket.h b/include/crow/websocket.h index 8d53c714e..22b083ba3 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -106,7 +106,7 @@ namespace crow signals_.clear(); - for (auto snum: handler_->signals()) + for (auto snum : handler_->signals()) signals_.add(snum); // Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== @@ -118,12 +118,13 @@ namespace crow s.getDigestBytes(digest); signals_.async_wait( - [&](const boost::system::error_code& e, int /*signal_number*/){ - if (!e){ - CROW_LOG_INFO << "Quitting Websocket: " << this; - close("Server Application Terminated"); - } - }); + [&](const boost::system::error_code& e, int /*signal_number*/) { + if (!e) + { + CROW_LOG_INFO << "Quitting Websocket: " << this; + close("Server Application Terminated"); + } + }); start(crow::utility::base64encode((unsigned char*)digest, 20)); } From cfc4281e3ba4b3eb372c9ed1338d6116a6abe27e Mon Sep 17 00:00:00 2001 From: The-EDev Date: Wed, 18 May 2022 13:49:50 +0300 Subject: [PATCH 5/6] Prevent acceptor from taking new connections while websockets close --- include/crow/http_server.h | 55 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/include/crow/http_server.h b/include/crow/http_server.h index a7002d778..7eefeb06c 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -161,7 +161,7 @@ namespace crow void stop() { - should_close_ = false; //Prevent the acceptor from taking new connections + should_close_ = true; //Prevent the acceptor from taking new connections while (handler_->websocket_count.load(std::memory_order_release) != 0) //Wait for the websockets to close properly { } @@ -207,33 +207,36 @@ namespace crow void do_accept() { - uint16_t service_idx = pick_io_service_idx(); - asio::io_service& is = *io_service_pool_[service_idx]; - task_queue_length_pool_[service_idx]++; - CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx]; + if (!should_close_) + { + uint16_t service_idx = pick_io_service_idx(); + asio::io_service& is = *io_service_pool_[service_idx]; + task_queue_length_pool_[service_idx]++; + CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx]; - auto p = new Connection( - is, handler_, server_name_, middlewares_, - get_cached_date_str_pool_[service_idx], *task_timer_pool_[service_idx], adaptor_ctx_, task_queue_length_pool_[service_idx]); + auto p = new Connection( + is, handler_, server_name_, middlewares_, + get_cached_date_str_pool_[service_idx], *task_timer_pool_[service_idx], adaptor_ctx_, task_queue_length_pool_[service_idx]); - acceptor_.async_accept( - p->socket(), - [this, p, &is, service_idx](boost::system::error_code ec) { - if (!ec) - { - is.post( - [p] { - p->start(); - }); - } - else - { - task_queue_length_pool_[service_idx]--; - CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx]; - delete p; - } - do_accept(); - }); + acceptor_.async_accept( + p->socket(), + [this, p, &is, service_idx](boost::system::error_code ec) { + if (!ec) + { + is.post( + [p] { + p->start(); + }); + } + else + { + task_queue_length_pool_[service_idx]--; + CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx]; + delete p; + } + do_accept(); + }); + } } private: From 5fcaa191637c04e1b1d2cf6c36aeaf2c5932d525 Mon Sep 17 00:00:00 2001 From: The-EDev Date: Wed, 18 May 2022 14:08:40 +0300 Subject: [PATCH 6/6] should_close_ -> shutting_down_ --- include/crow/http_server.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/crow/http_server.h b/include/crow/http_server.h index 7eefeb06c..56b24834b 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -161,7 +161,7 @@ namespace crow void stop() { - should_close_ = true; //Prevent the acceptor from taking new connections + shutting_down_ = true; //Prevent the acceptor from taking new connections while (handler_->websocket_count.load(std::memory_order_release) != 0) //Wait for the websockets to close properly { } @@ -207,7 +207,7 @@ namespace crow void do_accept() { - if (!should_close_) + if (!shutting_down_) { uint16_t service_idx = pick_io_service_idx(); asio::io_service& is = *io_service_pool_[service_idx]; @@ -245,7 +245,7 @@ namespace crow std::vector task_timer_pool_; std::vector> get_cached_date_str_pool_; tcp::acceptor acceptor_; - bool should_close_ = false; + bool shutting_down_ = false; boost::asio::signal_set signals_; boost::asio::deadline_timer tick_timer_;