Skip to content

Commit 6e8ce40

Browse files
committed
[coro_io] close idle coroutine by cancellation signal to avoid coroutine frame leak
1 parent eec1eda commit 6e8ce40

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

include/ylt/coro_io/client_pool.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include <ylt/util/expected.hpp>
4444

4545
#include "async_simple/Future.h"
46+
#include "async_simple/Signal.h"
47+
#include "async_simple/coro/LazyLocalBase.h"
4648
#include "async_simple/coro/Mutex.h"
4749
#include "coro_io.hpp"
4850
#include "detail/client_queue.hpp"
@@ -78,7 +80,11 @@ class client_pool : public std::enable_shared_from_this<
7880
while (true) {
7981
clients.reselect();
8082
self = nullptr;
81-
co_await coro_io::sleep_for(sleep_time);
83+
auto is_canceled = co_await coro_io::sleep_for(sleep_time);
84+
if (!is_canceled) {
85+
ELOG_TRACE << "coroutine destroyed, stop collect timeout client";
86+
break;
87+
}
8288
if ((self = self_weak.lock()) == nullptr) {
8389
break;
8490
}
@@ -336,6 +342,8 @@ class client_pool : public std::enable_shared_from_this<
336342
this->weak_from_this(), clients,
337343
(std::max)(collect_time, std::chrono::milliseconds{50}),
338344
pool_config_.idle_queue_per_max_clear_count)
345+
.setLazyLocal(
346+
async_simple::coro::LazyLocalBase{._slot = signal_.get()})
339347
.directlyStart(
340348
[](auto&&) {
341349
},
@@ -565,6 +573,7 @@ class client_pool : public std::enable_shared_from_this<
565573
}
566574

567575
const pool_config& get_pool_config() const noexcept { return pool_config_; }
576+
~client_pool() { signal_->emits(async_simple::SignalType::Terminate); }
568577

569578
private:
570579
template <typename, typename>
@@ -623,6 +632,8 @@ class client_pool : public std::enable_shared_from_this<
623632
std::atomic<uint64_t> timepoint_;
624633
ylt::util::atomic_shared_ptr<std::vector<asio::ip::tcp::endpoint>> eps_;
625634
async_simple::coro::Mutex dns_cache_update_mutex_;
635+
std::shared_ptr<async_simple::Signal> signal_ =
636+
async_simple::Signal::create();
626637
};
627638

628639
template <typename client_t,

0 commit comments

Comments
 (0)