#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <thread>
#include <unistd.h>
#ifdef BUILD_WITH_SYSTEMD_DAEMON
}
}
- 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 ...");
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;