<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/66974>66974</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            llvm_shutdown called by lld::exitLld(1) is racy
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld,
            lld:ELF
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          MaskRay
      </td>
    </tr>
</table>

<pre>
    `lld::fatal` calls `exitLld(1)`. `lld::error` when the error number reaches the limit calls `exitLld(1)` as well.
```
exitLld
  llvm_shutdown();
 llvm::sys::Process::Exit(val, /*NoCleanup=*/true);
```

If a worker thread created by `llvm/lib/Support/Parallel.cpp` calls `exitLld(1)`, `llvm_shutdown` will destroy
[the ManagedStatic instance in Executor::getDefaultExecutor](https://github.com/llvm/llvm-project/blob/486fc8158384d1c6758e5f9b856fc91606dab151/llvm/lib/Support/Parallel.cpp#L177).
If the main thread is blocked on [`L.sync()` in `TaskGroup::~TaskGroup`](https://github.com/llvm/llvm-project/blob/486fc8158384d1c6758e5f9b856fc91606dab151/llvm/lib/Support/Parallel.cpp#L205), it is possible that the main thread finishes first and exits with code 0, before the work thread calls `_Exit(1)`.
WHen this happens, the exit code of LLD will be 0 instead of the expected 1.

I have measured flakiness for ELF/invalid-eh-frame5.s and  ELF/invalid-eh-frame.s.
Sometimes the tests may exhibit 6 failures in 1000 runs, sometimes only 5 in 5000 runs.

@nga888 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVE-Pq7YX_TTO5mqQMcGBBYvMZPL7VcqrnjqVuny6mEtwx2Bkm8xk089eGcj8adWRuqsURRj7Hu45Puei9_o8EFUsv2f5YYNT6KyrvqF__gWvm9o214pJbkzDsj3L9i0GNExyUGiMByY5vepwMg0TRcpEySRP4GMBOWddLHjpaIDQEcxvYJj6mhw4QtWRnzeM7nX4AhjQwws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