<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Data race during the creation of the predefined units 5 and 6"
   href="https://bugs.llvm.org/show_bug.cgi?id=48987">48987</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Data race during the creation of the predefined units 5 and 6
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>flang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Runtime
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>rofirrim@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>David.Truby@arm.com, llvm-bugs@lists.llvm.org, sscalpone@nvidia.com, tkeith@nvidia.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24470" name="attach_24470" title="Delay setting unitMap until predefined units have been fully initialised">attachment 24470</a> <a href="attachment.cgi?id=24470&action=edit" title="Delay setting unitMap until predefined units have been fully initialised">[details]</a></span>
Delay setting unitMap until predefined units have been fully initialised

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).</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>