Skip to content

Commit 1a73c47

Browse files
authored
Merge pull request #938 from redboltz/fix_async_shutdown_cb_twice_call_problem
Fixed async_shutdown callback twice called problem.
2 parents 41a6937 + 4fdfb2e commit 1a73c47

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

include/mqtt/endpoint.hpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -5334,6 +5334,7 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
53345334
}
53355335
shutdown_requested_ = true;
53365336
mqtt_connected_ = false;
5337+
async_shutdown_handler_called_ = false;
53375338

53385339
MQTT_LOG("mqtt_impl", trace)
53395340
<< MQTT_ADD_VALUE(address, this)
@@ -5346,7 +5347,10 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
53465347
<< ec.message();
53475348
tim_shutdown_.cancel();
53485349
connected_ = false;
5349-
if (func) func(boost::system::errc::make_error_code(boost::system::errc::success));
5350+
if (!async_shutdown_handler_called_) {
5351+
async_shutdown_handler_called_ = true;
5352+
if (func) func(boost::system::errc::make_error_code(boost::system::errc::success));
5353+
}
53505354
}
53515355
);
53525356
// timeout timer set
@@ -5371,14 +5375,15 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
53715375
<< "post force_shutdown_and_close";
53725376
sp->socket().post(
53735377
[this, func = force_move(func), sp] {
5374-
if (connected_) {
5378+
if (!async_shutdown_handler_called_) {
53755379
error_code ec;
53765380
socket().force_shutdown_and_close(ec);
53775381
MQTT_LOG("mqtt_impl", trace)
53785382
<< MQTT_ADD_VALUE(address, this)
53795383
<< "force_shutdown_and_close ec:"
53805384
<< ec.message();
53815385
connected_ = false;
5386+
async_shutdown_handler_called_ = true;
53825387
if (func) func(boost::system::errc::make_error_code(boost::system::errc::success));
53835388
}
53845389
}
@@ -11591,6 +11596,7 @@ class endpoint : public std::enable_shared_from_this<endpoint<Mutex, LockGuard,
1159111596
std::atomic<bool> connected_{false};
1159211597
std::atomic<bool> mqtt_connected_{false};
1159311598
std::atomic<bool> shutdown_requested_{false};
11599+
std::atomic<bool> async_shutdown_handler_called_{false};
1159411600

1159511601
std::string client_id_;
1159611602

0 commit comments

Comments
 (0)