From ceab5a6f5276d40db7c4eb75e9a596791ebaa88a Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 1 Mar 2024 10:14:17 +0100 Subject: [PATCH] `ConfigObjectUtility`: Don't allow simultaneous creation of an object --- lib/remote/configobjectutility.cpp | 9 +++++++++ lib/remote/configobjectutility.hpp | 3 +++ 2 files changed, 12 insertions(+) diff --git a/lib/remote/configobjectutility.cpp b/lib/remote/configobjectutility.cpp index 9b5cf88b5ac..53b14898fb1 100644 --- a/lib/remote/configobjectutility.cpp +++ b/lib/remote/configobjectutility.cpp @@ -19,6 +19,8 @@ using namespace icinga; +ObjectNameMutex ConfigObjectUtility::m_ObjectCreateMutex; + String ConfigObjectUtility::GetConfigDir() { String prefix = ConfigPackageUtility::GetPackageDir() + "/_api/"; @@ -181,6 +183,13 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full { CreateStorage(); + // Lock the object name of the given type to prevent from being created concurrently. + m_ObjectCreateMutex.Lock(type, fullName); + + Defer unlockAndNotify([&type, &fullName]{ + m_ObjectCreateMutex.Unlock(type, fullName); + }); + { auto configType (dynamic_cast(type.get())); diff --git a/lib/remote/configobjectutility.hpp b/lib/remote/configobjectutility.hpp index 5a113c82edd..000660d487e 100644 --- a/lib/remote/configobjectutility.hpp +++ b/lib/remote/configobjectutility.hpp @@ -40,6 +40,9 @@ class ConfigObjectUtility static String EscapeName(const String& name); static bool DeleteObjectHelper(const ConfigObject::Ptr& object, bool cascade, const Array::Ptr& errors, const Array::Ptr& diagnosticInformation, const Value& cookie = Empty); + + // Ensures that an object is being created exclusively by one thread at any given time. + static ObjectNameMutex m_ObjectCreateMutex; }; }