Register as CPU inheritance destination in a separate thread 46/316846/3
authorKrzysztof Malysa <k.malysa@samsung.com>
Wed, 18 Dec 2024 10:12:39 +0000 (11:12 +0100)
committerKrzysztof Malysa <k.malysa@samsung.com>
Wed, 18 Dec 2024 12:12:48 +0000 (13:12 +0100)
This should prevent cynara from delaying its start due to the
registering delay that happens on VD systems.

Change-Id: I085f17b795de99a5addf2d7f72e337a8cba20329

src/service/main/main.cpp

index 05d248f8689045fcc0a4e275276b7b2eca3c1525..22294506f6e71e0554a7631e771a783113803caf 100644 (file)
@@ -37,6 +37,7 @@
 #include <sys/resource.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <thread>
 #include <unistd.h>
 
 #ifdef BUILD_WITH_SYSTEMD_DAEMON
@@ -137,12 +138,21 @@ int main(int argc, char **argv) {
             }
         }
 
-        resource_pid_t self_data;
-        self_data.pid = getpid();
-        LOGW("Registering as CPU inheritance destination...");
-        if (resource_register_cpu_inheritance_destination(Cynara::RESOURCE_CPU_DEST_NAME,
-                                                          self_data) != 0)
-            LOGE("resource_register_cpu_inheritance_destination failed");
+        // Do this in a separate thread not to delay Cynara startup, because on VD systems these
+        // resource_register_cpu_inheritance_destination() call can take even 1s, causing clients to
+        // error out due to cynara not yet listening on its sockets.
+        // The thread is not detached because if the main thread dies first, the logging facilities
+        // are destroyed, so logging is UB in the other thread.
+        auto cpuInheritanceThread = std::thread{[] {
+            resource_pid_t self_data;
+            self_data.pid = getpid();
+            LOGW("Registering as CPU inheritance destination...");
+            if (resource_register_cpu_inheritance_destination(Cynara::RESOURCE_CPU_DEST_NAME,
+                                                              self_data) != 0)
+                LOGE("resource_register_cpu_inheritance_destination failed");
+            else
+                LOGW("Registered as CPU inheritance destination.");
+        }};
 
         Cynara::Cynara cynara;
         LOGW("Cynara service is starting ...");
@@ -165,8 +175,12 @@ int main(int argc, char **argv) {
         cynara.finalize();
         LOGW("Cynara service is stopped");
 
+        cpuInheritanceThread.join();
+        LOGW("Unregistering as CPU inheritance destination...");
         if(resource_unregister_cpu_inheritance_destination(Cynara::RESOURCE_CPU_DEST_NAME) != 0)
             LOGE("resource_unregister_cpu_inheritance_destination failed");
+        else
+            LOGW("Unregistered as CPU inheritance destination.");
     } catch (std::exception &e) {
         LOGC("Cynara stoped because of unhandled exception: %s", e.what());
         return EXIT_FAILURE;