From 69a25b2dc2fb4e96bc3b451cf7d4b92b68b4e58e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Wed, 12 Jun 2024 22:00:17 -0400 Subject: [PATCH] [network] Improve device release safety --- .../score-lib-device/Device/Protocol/DeviceInterface.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/plugins/score-lib-device/Device/Protocol/DeviceInterface.cpp b/src/plugins/score-lib-device/Device/Protocol/DeviceInterface.cpp index 8ea9045a85..ff5b83e970 100644 --- a/src/plugins/score-lib-device/Device/Protocol/DeviceInterface.cpp +++ b/src/plugins/score-lib-device/Device/Protocol/DeviceInterface.cpp @@ -1019,21 +1019,22 @@ void releaseDevice( { if(dd) { + auto shared_dd = std::shared_ptr(std::move(dd)); auto strand = boost::asio::make_strand(ctx.context); - boost::asio::dispatch(ctx.context, [&dev = dd] { dev->get_protocol().stop(); }); + boost::asio::dispatch(strand, [dev = shared_dd] { dev->get_protocol().stop(); }); std::future wait1 - = boost::asio::dispatch(ctx.context, boost::asio::use_future); + = boost::asio::dispatch(strand, boost::asio::use_future); if(auto res = wait1.wait_for(std::chrono::seconds(1)); res != std::future_status::ready) { qDebug() << "Device deletion: asio thread seems stuck (1)"; } - boost::asio::dispatch(ctx.context, [d = std::move(dd)]() mutable { d.reset(); }); + boost::asio::dispatch(strand, [d = std::move(shared_dd)]() mutable { d.reset(); }); std::future wait2 - = boost::asio::dispatch(ctx.context, boost::asio::use_future); + = boost::asio::dispatch(strand, boost::asio::use_future); if(auto res = wait2.wait_for(std::chrono::seconds(1)); res != std::future_status::ready) {