From 1245ae05c6e2ca7a2af055f9c44f19a0db2971a5 Mon Sep 17 00:00:00 2001
From: yangbin <robin.yb@huawei.com>
Date: Thu, 15 Aug 2019 15:24:03 +0800
Subject: [PATCH 3/3] systemd-core: Close and free dbus when bus authenticating
 timedout

1. when timedout happened on authenticating a private dbus(can be established by systemctl command),
this dbus will never be freed and closed, and will left on systemd permanently even through the client
(for example, systemctl command) has closed the connection. This is because when timedout happend,
the event and also the timer to watch dbus actions is disabled by sd_event_source_set_enabled
from source_dispatch function, and systemd can do nothing on it since this dbus will not be activated again.
2. If a private dbus staying on authenticating state, and when systemd sends a signal message, it will also
add this message to the message write queue of this bus and will never send it out because the dbus is not in running.
systemd does this for it believe that the bus will change from authenticating to running sometime, but actually it will not.
3. When many private dbuses are left as authenticating and many signal messages are sent from dbus, it will eat up our memory
to hold these dbuses and messages, and memory usage of systemd will grow very fast.
4. This patch fix this problem by closing and freeing the dbus when authenticating timedout.
---
 src/libsystemd/sd-bus/sd-bus.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
index 05cb4c3..65cf449 100644
--- a/src/libsystemd/sd-bus/sd-bus.c
+++ b/src/libsystemd/sd-bus/sd-bus.c
@@ -2946,6 +2946,11 @@ static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priorit
         if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
                 bus_enter_closing(bus);
                 r = 1;
+        } else if(r == -ETIMEDOUT && !bus->is_system) {
+                /*close dbus directly when timedout happened and it is a private dbus*/
+                log_info("Private bus is closed due authentication timedout.");
+                bus_enter_closing(bus);
+                r = 1;
         } else if (r < 0)
                 return r;
 
-- 
2.17.1

