From 192c358160efebf47be45a230009fed0e54fcdef Mon Sep 17 00:00:00 2001 From: Dave Streeter Date: Wed, 5 Mar 2025 13:41:53 +0000 Subject: [PATCH] HPCC-33586 Fix double-checked locking pattern for CNamedGroupStore in Dali Change groupStore to be an atomic pointer for thread safety. Signed-off-by: Dave Streeter --- dali/base/dadfs.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/dali/base/dadfs.cpp b/dali/base/dadfs.cpp index 0b44a692229..7752a195a3e 100644 --- a/dali/base/dadfs.cpp +++ b/dali/base/dadfs.cpp @@ -8239,20 +8239,22 @@ class CNamedGroupStore: implements INamedGroupStore, public CInterface }; -static CNamedGroupStore *groupStore = NULL; +static std::atomic groupStore{nullptr}; static CriticalSection groupsect; bool CNamedGroupIterator::match() { if (conn.get()) { if (matchgroup.get()) { - if (!groupStore) + CLeavableCriticalBlock block3(groupsect); + if (!groupStore.load()) return false; const char *name = pe->query().queryProp("@name"); if (!name||!*name) return false; GroupType dummy; - Owned lgrp = groupStore->dolookup(name, conn, NULL, dummy); + Owned lgrp = groupStore.load()->dolookup(name, conn, NULL, dummy); + block3.leave(); if (lgrp) { if (exactmatch) return lgrp->equals(matchgroup); @@ -8266,14 +8268,13 @@ bool CNamedGroupIterator::match() return false; } -INamedGroupStore &queryNamedGroupStore() +INamedGroupStore &queryNamedGroupStore() { - if (!groupStore) { - CriticalBlock block(groupsect); - if (!groupStore) - groupStore = new CNamedGroupStore(); + CriticalBlock block(groupsect); + if (!groupStore.load()) { + groupStore.store(new CNamedGroupStore()); } - return *groupStore; + return *(groupStore.load()); } // -------------------------------------------------------- @@ -9244,8 +9245,8 @@ void closedownDFS() // called by dacoven } DFdir = NULL; CriticalBlock block2(groupsect); - ::Release(groupStore); - groupStore = NULL; + ::Release(groupStore.load()); + groupStore.store(nullptr); } class CDFPartFilter : implements IDFPartFilter, public CInterface