Make sure the process would not be restarted too frequently 39/300639/1
authorJi-hoon Lee <dalton.lee@samsung.com>
Thu, 30 Mar 2023 11:25:22 +0000 (20:25 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Mon, 30 Oct 2023 10:27:01 +0000 (19:27 +0900)
If an auto-restart process gets restarted more than 5 times
in 10 seconds, it is considered to be an abnormal behavior
and will not be restarted automatically anymore.

For this reason, modified the app_restart() function to sleep
if the application tries to restart in less than 3 seconds
from the time it was launched.

Change-Id: I44bbb4d24bde28cf35f9927be3dc7bb8c7145f81

inc/service_main.h
src/service_main.cpp

index 135e3d4..5d774f9 100644 (file)
@@ -18,6 +18,7 @@
 #ifndef __SERVICE_MAIN_H__
 #define __SERVICE_MAIN_H__
 
+#include <chrono>
 #include <string>
 #include <vector>
 
@@ -226,6 +227,8 @@ private:
        IPreferenceManager& mPreferenceManager;
 
        pid_t mLastDuckingRequester{-1};
+
+       std::chrono::time_point<std::chrono::steady_clock> mStartedTime;
 };
 
 #endif /* __SERVICE_MAIN_H__ */
index 20854d0..4886872 100644 (file)
 #include <string.h>
 #include <glib.h>
 
-#include <chrono>
 #include <ctime>
 #include <list>
 #include <sstream>
+#include <thread>
 #include <tuple>
 #include <vector>
 
@@ -1387,6 +1387,7 @@ bool CServiceMain::app_create(void *data)
        // Todo: add your code here.
        MAS_LOGE("[ENTER] Service app create");
 
+       mStartedTime = std::chrono::steady_clock::now();
        g_service_main = this;
 
        mClientManager.set_application_manager(&mApplicationManager);
@@ -1464,7 +1465,24 @@ void CServiceMain::app_terminate(void *data)
 
 void CServiceMain::app_restart()
 {
+       MAS_LOGE("[ENTER]");
+
+       /* It would be best if we could sleep only when the current restart request
+        * is the 5th operation within 10 seconds, however we don't have a way to
+        * determine the number of restart operations since such information has to be
+        * stored externally but there isn't appropriate place to store it at the moment. */
+       const std::chrono::seconds minimum_interval(3);
+       auto interval = std::chrono::steady_clock::now() - mStartedTime;
+       if (interval < minimum_interval) {
+               MAS_LOGE("sleeping for %lld ms",
+                       std::chrono::duration_cast<std::chrono::milliseconds>(minimum_interval - interval).count());
+               std::this_thread::sleep_for(minimum_interval - interval);
+       } else {
+               MAS_LOGE("interval is %lld ms, no need to sleep",
+                       std::chrono::duration_cast<std::chrono::milliseconds>(interval).count());
+       }
        mServiceIpc.send_restart_notification();
+
        service_app_exit();
 }