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 6, 2024
1 parent a22260e commit a259a68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/remote/configobjectutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "remote/configobjectutility.hpp"
#include "remote/configpackageutility.hpp"
#include "remote/apilistener.hpp"
#include "config/configcompiler.hpp"
#include "config/configitem.hpp"
#include "base/atomic-file.hpp"
Expand All @@ -19,6 +18,8 @@

using namespace icinga;

ObjectNameMutex ConfigObjectUtility::m_ObjectCreateMutex;

String ConfigObjectUtility::GetConfigDir()
{
String prefix = ConfigPackageUtility::GetPackageDir() + "/_api/";
Expand Down Expand Up @@ -181,6 +182,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<ConfigType*>(type.get()));

Expand Down
4 changes: 4 additions & 0 deletions lib/remote/configobjectutility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef CONFIGOBJECTUTILITY_H
#define CONFIGOBJECTUTILITY_H

#include "remote/apilistener.hpp"
#include "remote/i2-remote.hpp"
#include "base/array.hpp"
#include "base/configobject.hpp"
Expand Down Expand Up @@ -40,6 +41,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;
};

}
Expand Down

0 comments on commit a259a68

Please sign in to comment.