From cf7d2e49ff0f581bd2f0ffcaf29d719b337f4b9b Mon Sep 17 00:00:00 2001 From: tilsche Date: Mon, 12 Mar 2018 10:32:09 +0100 Subject: [PATCH 1/2] allow for a clean shutdown of asio connections remove heartbeat timer on close make sure only weak references are stored in bindings even below C++17 --- include/amqpcpp/libboostasio.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/include/amqpcpp/libboostasio.h b/include/amqpcpp/libboostasio.h index 613ff73..c1492f5 100644 --- a/include/amqpcpp/libboostasio.h +++ b/include/amqpcpp/libboostasio.h @@ -36,9 +36,9 @@ // C++17 has 'weak_from_this()' support. #if __cplusplus >= 201701L -#define PTR_FROM_THIS weak_from_this +#define PTR_FROM_THIS(T) weak_from_this() #else -#define PTR_FROM_THIS shared_from_this +#define PTR_FROM_THIS(T) std::weak_ptr(shared_from_this()) #endif /** @@ -142,7 +142,7 @@ protected: this, _1, _2, - PTR_FROM_THIS(), + PTR_FROM_THIS(Watcher), connection, fd); return get_dispatch_wrapper(fn); @@ -160,7 +160,7 @@ protected: this, _1, _2, - PTR_FROM_THIS(), + PTR_FROM_THIS(Watcher), connection, fd); return get_dispatch_wrapper(fn); @@ -346,7 +346,7 @@ protected: const auto fn = boost::bind(&Timer::timeout, this, _1, - PTR_FROM_THIS(), + PTR_FROM_THIS(Timer), connection, timeout); @@ -583,6 +583,16 @@ public: * Destructor */ ~LibBoostAsioHandler() override = default; + + /** + * Make sure to stop the heartbeat timer after the connection is closed. + * Otherwise it will keep the service running forever. + */ + void onClosed(TcpConnection* connection) override + { + (void)connection; + _timer.reset(); + } }; From 8a6adae8e6c8bb6e5b1ffe32a6a196b8e9492816 Mon Sep 17 00:00:00 2001 From: tilsche Date: Mon, 12 Mar 2018 11:57:53 +0100 Subject: [PATCH 2/2] Remove work from libboostasio example as it keeps the service alive even after everything is closed. The example now terminates properly. --- examples/libboostasio.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/libboostasio.cpp b/examples/libboostasio.cpp index c71c9e3..d290382 100644 --- a/examples/libboostasio.cpp +++ b/examples/libboostasio.cpp @@ -30,9 +30,6 @@ int main() // note: we suggest use of 2 threads - normally one is fin (we are simply demonstrating thread safety). boost::asio::io_service service(4); - // create a work object to process our events. - boost::asio::io_service::work work(service); - // handler for libev AMQP::LibBoostAsioHandler handler(service);