Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Proposition) Closing asio::connection, socket handling #63

Open
vsabadazh opened this issue Jun 2, 2021 · 0 comments
Open

(Proposition) Closing asio::connection, socket handling #63

vsabadazh opened this issue Jun 2, 2021 · 0 comments

Comments

@vsabadazh
Copy link

Hello,

In our company, we have faced an issue which would lead our application to crash in certain circumstances (e.g. some of the signals keep coming after the connection was closed). We have modified the sdbusplus::asio::connection class so it handles the underlying socket in a more explicit way to close it, see below:

diff --git a/include/sdbusplus/asio/connection.hpp b/include/sdbusplus/asio/connection.hpp
index 15edd1b..b11a3d2 100644
--- a/include/sdbusplus/asio/connection.hpp
+++ b/include/sdbusplus/asio/connection.hpp
@@ -62,6 +62,12 @@ class connection : public sdbusplus::bus::bus
         socket.assign(get_fd());
         read_wait();
     }
+    connection(boost::asio::io_context& io, bus &&bus) :
+        sdbusplus::bus::bus(std::move(bus)), io_(io), socket(io_)
+    {
+        socket.assign(get_fd());
+        read_wait();
+    }
     ~connection()
     {
         // The FD will be closed by the socket object, so assign null to the
@@ -70,6 +76,13 @@ class connection : public sdbusplus::bus::bus
         socket.release();
     }
 
+    void close()
+    {
+        socket.cancel();
+        socket.release();
+        sdbusplus::bus::bus::close();
+    }
+
     /** @brief Perform an asynchronous send of a message, executing the handler
      *         upon return and return
      *
@@ -351,6 +364,10 @@ class connection : public sdbusplus::bus::bus
     void read_immediate()
     {
         boost::asio::post(io_, [&] {
+            if (!socket.is_open())
+            {
+                return;
+            }
             if (process_discard())
             {
                 read_immediate();

Applying it will improve stability of the library, which it did in our case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant