llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Data race during the creation of the predefined units 5 and 6

Open rofirrim opened this issue 5 years ago • 1 comments

Bugzilla Link 48987
Version unspecified
OS Linux
Attachments Delay setting unitMap until predefined units have been fully initialised, Unit test showcasing the issue
CC @sscalpone,@tskeith

Extended Description

Hi,

function ExternalFileUnit::GetUnitMap in unit.cpp uses a CriticalSection RAII object to serialise the initialisation of the unit map.

However it sets the global unitMap variable too early before the initialisation of the predefined units 6 (stdout) and 5 (stdin).

In machines with lots of cores (we've been able to reproduce this in a 32-core machine) threads printing for the first time in parallel may observe the predefined units before they are fully initialised.

The observed failure is that a function like BeginExternalListOutput invokes SetDirection. When using a PRINT statement this uses the default unit which BeginExternaListOutput will map to the unit id 6. SetDirection checks that the requested direction makes sense. E.g. for unit 6 this is "mayWrite()", however because the unit 6 has not been fully initialised yet the check returns false and an error is signaled.

The attached patch keeps the unit map in a local variable once the predefined units have been initialized. Because ExternalFileUnit::CreateNew invokes ExternalFileUnit::GetUnitMap that would immediately lead to deadlock. We overloaded CreateNew to received the UnitMap* and avoid the deadlock.

We can't see other calls to CreateNew in the code as it is now so the overload may not bee needed, but we are not sure if other calls to CreateNew may be needed in the future.

I am not sure what is the policy at the moment, but I understand that we can proceed with a patch in phabricator for flang (not sure if this would impacts plans regarding fir-dev).

rofirrim avatar Feb 01 '21 11:02 rofirrim

This issue appears to have been fixed by @klausler in https://reviews.llvm.org/D101928, so I'll close it. Please don't hesitate to reopen if the issue persists.

ekieri avatar Apr 14 '22 15:04 ekieri