Skip to content

Commit

Permalink
Properly lock access to listenersMap (#715)
Browse files Browse the repository at this point in the history
- Prevents check-then-act race while iterating over the pairs
  in the map

Signed-off-by: The MathWorks, Inc. <alchrist@mathworks.com>
  • Loading branch information
achristoforides committed Aug 21, 2022
1 parent be23105 commit 6b29e26
Showing 1 changed file with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
=============================================================================*/

#include "ConfigurationNotifier.hpp"
#include "../ComponentRegistry.hpp"
#include "../metadata/ComponentMetadata.hpp"
#include "ComponentConfigurationImpl.hpp"
#include "ComponentManagerImpl.hpp"
#include "ConfigurationNotifier.hpp"
#include "cppmicroservices/SecurityException.h"
#include "cppmicroservices/SharedLibraryException.h"
#include "cppmicroservices/asyncworkservice/AsyncWorkService.hpp"
#include "cppmicroservices/cm/ConfigurationAdmin.hpp"
#include "cppmicroservices/SecurityException.h"

namespace cppmicroservices {
namespace scrimpl {
Expand Down Expand Up @@ -200,18 +200,14 @@ void ConfigurationNotifier::NotifyAllListeners(
ConfigChangeNotification notification =
ConfigChangeNotification(pid, std::move(properties), std::move(type));

std::shared_ptr<TokenMap> listenersMapCopy;
{
auto listenersMapHandle = listenersMap.lock();
auto iter = listenersMapHandle->find(pid);
if (iter != listenersMapHandle->end()) {
listenersMapCopy = iter->second;
} else {
return;
auto listenersMapHandle = listenersMap.lock();
auto iter = listenersMapHandle->find(pid);
if (iter != listenersMapHandle->end()) {
for (const auto& configListenerPtr : *(iter->second)) {
configListenerPtr.second.notify(notification);
}
}
for (const auto& configListenerPtr : *listenersMapCopy) {
configListenerPtr.second.notify(notification);
} else {
return;
}
}

Expand Down

0 comments on commit 6b29e26

Please sign in to comment.