Skip to content

Commit 9aabc10

Browse files
Fix/asio scheduler cancellation (#81)
* test - AsioScheduler called onTimer after being destructed Signed-off-by: Alexey-N-Chernyshov <[email protected]> * fix AsioScheduler Signed-off-by: Alexey-N-Chernyshov <[email protected]> * add comment Signed-off-by: Alexey-N-Chernyshov <[email protected]> * add correct asio_scheduler cancellation From boost docs: "These (scheduled after cancel() called) handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation." Adds shared_pointer flag for cancellation. Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent 8ea1af5 commit 9aabc10

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

include/libp2p/protocol/common/asio/asio_scheduler.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ namespace libp2p::protocol {
1414

1515
class AsioScheduler : public Scheduler {
1616
public:
17-
~AsioScheduler() override = default;
18-
1917
AsioScheduler(boost::asio::io_context &io, SchedulerConfig config);
2018

19+
~AsioScheduler() override;
20+
2121
private:
2222
Ticks now() const override;
2323

@@ -31,6 +31,17 @@ namespace libp2p::protocol {
3131
Ticks interval_;
3232
boost::asio::deadline_timer timer_;
3333
boost::posix_time::ptime started_;
34+
35+
/*
36+
* Timer callback is canceled
37+
*
38+
* In case when the timer has already expired when cancel() is called and
39+
* the handlers have been invoked or queued for invocation. These handlers
40+
* can no longer be cancelled, and therefore are passed an error code that
41+
* indicates the successful completion of the wait operation. Cancel is used
42+
* to indicate that handler should not be executed.
43+
*/
44+
std::shared_ptr<bool> canceled_;
3445
std::function<void(const boost::system::error_code &)> timer_cb_;
3546
std::function<void()> immediate_cb_;
3647
bool immediate_cb_scheduled_ = false;

src/protocol/common/asio/asio_scheduler.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,22 @@ namespace libp2p::protocol {
1515
interval_(config.period_msec),
1616
timer_(io, boost::posix_time::milliseconds(interval_)),
1717
started_(boost::posix_time::microsec_clock::local_time()),
18-
timer_cb_([this](const boost::system::error_code &error) {
19-
if (!error)
18+
canceled_(std::make_shared<bool>(false)),
19+
timer_cb_([this, canceled{canceled_}](
20+
const boost::system::error_code &error) {
21+
if (!error && !*canceled)
2022
onTimer();
2123
}),
2224
immediate_cb_([this] { onImmediate(); }) {
2325
assert(interval_ > 0 && interval_ <= 1000);
2426
timer_.async_wait(timer_cb_);
2527
}
2628

29+
AsioScheduler::~AsioScheduler() {
30+
*canceled_ = true;
31+
timer_.cancel();
32+
};
33+
2734
Scheduler::Ticks AsioScheduler::now() const {
2835
boost::posix_time::ptime t(boost::posix_time::microsec_clock::local_time());
2936
return (t - started_).total_milliseconds();

0 commit comments

Comments
 (0)