Skip to content

Commit

Permalink
ConfigObjectUtility: Don't allow simultaneous creation of an object
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Mar 1, 2024
1 parent eed2a1f commit c93869d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/remote/configobjectutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using namespace icinga;

ObjectNameMutex ConfigObjectUtility::m_ObjectCreateLock;

String ConfigObjectUtility::GetConfigDir()
{
String prefix = ConfigPackageUtility::GetPackageDir() + "/_api/";
Expand Down Expand Up @@ -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_ObjectCreateLock.Lock(type, fullName);

Defer unlockAndNotify([&type, &fullName]{
m_ObjectCreateLock.Unlock(type, fullName);
});

{
auto configType (dynamic_cast<ConfigType*>(type.get()));

Expand Down
3 changes: 3 additions & 0 deletions lib/remote/configobjectutility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_ObjectCreateLock;
};

}
Expand Down

0 comments on commit c93869d

Please sign in to comment.