Segfault when concurrently delete TMemFile

Hi,

I have a segmentation fault when I concurrently delete TMemFiles which have a TTree (even empty) inside. If I surround the delete instruction with TThread::Lock-UnLock, I don’t have any issue.

Minimal code to reproduce the segfault:

void *thread(void * ptri){
	Long_t tid = (Long_t) ptri;
	Info("Thread", "tid: %d", tid);
	
	char fname[10];
	sprintf(fname, "f%d.root", tid);
	TMemFile *memFile = new TMemFile(fname, "new");
	
	char tname[10];
	sprintf(tname, "tree_%d", tid);
	TTree *tree = new TTree(tname, tname);

	//TThread::Lock();
	delete memFile;
	//TThread::UnLock();
}

void threadsafe_issue(){
	Int_t nbThread = 20;
	TThread::Initialize();
	
	TThread **threads = new TThread*[nbThread];	
	for(Long_t i=0 ; i<nbThread ; i++){
		char tname[5];
		sprintf(tname, "h_%d", i);
		threads[i] = new TThread(tname, thread, (void *) i);
		threads[i]->Run();		
	}
	
	for(Long_t i=0 ; i<nbThread ; i++){
		threads[i]->Join();
		delete threads[i];
	}
	delete[] threads;
}

and the ROOT ouput, segfault.txt (12.2 KB).

Thank you.

Hi,

Which version of ROOT are you using?

Cheers,
Philippe.

Hi,

I am using version 5.34/21.

It is related to this bug report https://sft.its.cern.ch/jira/browse/ROOT-6872.

Cheers,
Romain

This has been fixed in v6.12/02 by a complex and significant overall of the ROOT global locks and its use within the global collection and RecursiveRemove. Due to the complexity we do not plan on backporting this update/fix to v5.34.

Thanks,
Philippe.

PS. To work-around the issue you need to lock both the construction and destruction of the TMemFile.