Skip to content

Commit

Permalink
<fix>(timer): fix that timer still work after stopping wsService (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasLi1024 committed May 22, 2023
1 parent 07d5d79 commit 5d8fb05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
15 changes: 12 additions & 3 deletions bcos-boostssl/utility/NewTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
*/
#pragma once
#include "bcos-utilities/BoostLog.h"
#include "bcos-utilities/Common.h"
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include "bcos-utilities/Common.h"
#include <exception>

namespace bcos
Expand Down Expand Up @@ -90,6 +90,11 @@ class Timer : public std::enable_shared_from_this<Timer>
return;
}

if (m_ioService)
{
m_ioService->stop();
}

if (m_delayHandler)
{
m_delayHandler->cancel();
Expand All @@ -99,6 +104,8 @@ class Timer : public std::enable_shared_from_this<Timer>
{
m_timerHandler->cancel();
}

m_running = false;
}

private:
Expand Down Expand Up @@ -201,10 +208,9 @@ class TimerFactory
return timer;
}

private:
void startThread()
{
if (m_worker)
if (m_worker || !m_running)
{
return;
}
Expand Down Expand Up @@ -248,7 +254,9 @@ class TimerFactory
m_running = false;
BCOS_LOG(INFO) << LOG_BADGE("stopThread") << LOG_DESC("stop the timer thread");

// must stop ioService, otherwise thread won't stop
m_ioService->stop();

if (m_worker->get_id() != std::this_thread::get_id())
{
m_worker->join();
Expand All @@ -260,6 +268,7 @@ class TimerFactory
}
}

private:
std::atomic_bool m_running = {false};
std::string m_threadName = "timerFactory";
std::unique_ptr<std::thread> m_worker = nullptr;
Expand Down
18 changes: 12 additions & 6 deletions bcos-boostssl/websocket/WsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void WsService::start()
{
m_timerFactory = std::make_shared<timer::TimerFactory>();
}
m_timerFactory->startThread();

// start ioc thread
if (m_ioservicePool)
Expand Down Expand Up @@ -133,12 +134,6 @@ void WsService::stop()
}
m_running = false;

// stop ioc thread
if (m_ioservicePool)
{
m_ioservicePool->stop();
}

if (m_statTimer)
{
m_statTimer->stop();
Expand All @@ -149,6 +144,17 @@ void WsService::stop()
m_reconnectTimer->stop();
}

// stop ioc thread
if (m_ioservicePool)
{
m_ioservicePool->stop();
}

if (m_timerFactory)
{
m_timerFactory->stopThread();
}

WEBSOCKET_SERVICE(INFO) << LOG_BADGE("stop") << LOG_DESC("stop websocket service successfully");
}

Expand Down

0 comments on commit 5d8fb05

Please sign in to comment.