Handle the container restart from within gracefully with DBUS reconnection 17/21417/5
authorLukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
Tue, 20 May 2014 07:15:57 +0000 (09:15 +0200)
committerLukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
Wed, 21 May 2014 14:35:17 +0000 (16:35 +0200)
[Bug/Feature]   You can now restart the container from within.
[Cause]         Restarting was stopped by the reconnection routine.
[Solution]      Reconnect tries few times with greater timeout before
                giving up. This gives the time for the container to restart.
[Verification]  Built, installed, run tests and the daemon.

Change-Id: I0e4f1c248af9f0d2faf81662876b971c2b35ed02
Signed-off-by: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
server/container.cpp

index a4ad5f1..6ce6f24 100644 (file)
 namespace security_containers {
 
 
+namespace {
+
+// TODO: move constants to the config file when default values are implemented there
+const int RECONNECT_RETRIES = 15;
+const int RECONNECT_DELAY = 1 * 1000;
+
+} // namespace
+
 Container::Container(const std::string& containerConfigPath)
 {
     mConfig.parseFile(containerConfigPath);
@@ -136,26 +144,30 @@ void Container::onNameLostCallback()
 
 void Container::reconnectHandler()
 {
-    std::string address;
-
     mConnection.reset();
 
-    try {
-        address = mConnectionTransport->acquireAddress();
-    } catch (SecurityContainersException&) {
-        LOGE(getId() << "The socket does not exist anymore, something went terribly wrong, stopping the container");
-        stop();
-        return;
+    for (int i = 0; i < RECONNECT_RETRIES; ++i) {
+        // This sleeps even before the first try to give DBUS some time to come back up
+        std::this_thread::sleep_for(std::chrono::milliseconds(RECONNECT_DELAY));
+
+        if (isStopped()) {
+            LOGI(getId() << ": Has stopped, nothing to reconnect to, bailing out");
+            return;
+        }
+
+        try {
+            LOGT(getId() << ": Reconnect try " << i+1);
+            mConnection.reset(new ContainerConnection(mConnectionTransport->acquireAddress(),
+                                                      std::bind(&Container::onNameLostCallback, this)));
+            LOGI(getId() << ": Reconnected");
+            return;
+        } catch (SecurityContainersException&) {
+            LOGT(getId() << ": Reconnect try " << i+1 << " has been unsuccessful");
+        }
     }
 
-    try {
-        mConnection.reset(new ContainerConnection(address, std::bind(&Container::onNameLostCallback, this)));
-        LOGI(getId() << ": Reconnected");
-    } catch (SecurityContainersException&) {
-        LOGE(getId() << ": Reconnecting to the DBUS has failed, stopping the container");
-        stop();
-        return;
-    }
+    LOGE(getId() << ": Reconnecting to the DBUS has failed, stopping the container");
+    stop();
 }