@@ -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
0 commit comments