Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPCC-33586 fix double checked locking pattern for CNamedGroupStore in dali #19585

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions dali/base/dadfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8239,20 +8239,22 @@ class CNamedGroupStore: implements INamedGroupStore, public CInterface

};

static CNamedGroupStore *groupStore = NULL;
static std::atomic<CNamedGroupStore *> groupStore{nullptr};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a mixture of fixes here. Either the variable should be atomic, or it should be accessed within a critical section. No need to do both.

static CriticalSection groupsect;

bool CNamedGroupIterator::match()
{
if (conn.get()) {
if (matchgroup.get()) {
if (!groupStore)
CLeavableCriticalBlock block3(groupsect);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This critical section will be held over the call to dolookup. That is likely to cause extra thread contention.

If you are worried about groupStore being destroyed, a more efficient solution is to copy and link it with the critical block, and then use the linked pointer. However, see note on closedownDFS() below - in which case it can never be freed.

if (!groupStore.load())
return false;
const char *name = pe->query().queryProp("@name");
if (!name||!*name)
return false;
GroupType dummy;
Owned<IGroup> lgrp = groupStore->dolookup(name, conn, NULL, dummy);
Owned<IGroup> lgrp = groupStore.load()->dolookup(name, conn, NULL, dummy);
block3.leave();
if (lgrp) {
if (exactmatch)
return lgrp->equals(matchgroup);
Expand All @@ -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());
}

// --------------------------------------------------------
Expand Down Expand Up @@ -9204,7 +9205,7 @@ GetFileClusterNamesType CDistributedFileDirectory::getFileClusterNames(const cha
// --------------------------------------------------------


static CDistributedFileDirectory *DFdir = NULL;
static std::atomic<CDistributedFileDirectory *> DFdir{nullptr};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comments to above - either make it atomic, or protect all access within a critical section.

static CriticalSection dfdirCrit;

/**
Expand All @@ -9214,12 +9215,10 @@ static CriticalSection dfdirCrit;
*/
IDistributedFileDirectory &queryDistributedFileDirectory()
{
if (!DFdir) {
CriticalBlock block(dfdirCrit);
if (!DFdir)
DFdir = new CDistributedFileDirectory();
}
return *DFdir;
CriticalBlock block(dfdirCrit);
if (!DFdir.load())
DFdir.store(new CDistributedFileDirectory());
return *DFdir.load();
}

/**
Expand All @@ -9229,7 +9228,7 @@ void closedownDFS() // called by dacoven
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is closedownDFS() ever called - I don't think so. If not the function can be deleted, avoiding the lifetime problem above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closedownDFS() is called from dali/base/dacoven.cpp :

image

CriticalBlock block(dfdirCrit);
try {
delete DFdir;
delete DFdir.load();
}
catch (IMP_Exception *e) {
if (e->errorCode()!=MPERR_link_closed)
Expand All @@ -9242,10 +9241,10 @@ void closedownDFS() // called by dacoven
throw;
e->Release();
}
DFdir = NULL;
DFdir.store(nullptr);
CriticalBlock block2(groupsect);
::Release(groupStore);
groupStore = NULL;
::Release(groupStore.load());
groupStore.store(nullptr);
}

class CDFPartFilter : implements IDFPartFilter, public CInterface
Expand Down
Loading