<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 - std::filesystem::weakly_canonical race condition when the underlying path is removed"
   href="https://bugs.llvm.org/show_bug.cgi?id=49493">49493</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::filesystem::weakly_canonical race condition when the underlying path is removed
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>xavier.roche@algolia.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24609" name="attach_24609" title="Test aimed to reproduce the race issue">attachment 24609</a> <a href="attachment.cgi?id=24609&action=edit" title="Test aimed to reproduce the race issue">[details]</a></span>
Test aimed to reproduce the race issue

The std::filesystem::weakly_canonical function is normally able to canonize
both non-existing path(es) and existing path(es).

However, the current implementation is having issues with canonizing a path
that disappears while the function is being called, leading to randomly
returning errors (and/or raising filesystem_error).

Important Note: this MAY be an expected behavior, as the official specification
of std::filesystem::weakly_canonical does not appear to be completely clear
towards impact of filesystem volatility.

The attached sample demonstrate the issue:
-----------------------------------------

clang++ -g3 -pthread -std=c++20 filesystem_weakly_canonical-race-canonical.cpp
-o filesystem_weakly_canonical-race-canonical
./filesystem_weakly_canonical-race-canonical

Starting computing
terminate called after throwing an instance of
'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: cannot make canonical path: No such file or
directory [foo]
Aborted (core dumped)

Our understanding is that std::filesystem::weakly_canonical calls
std::filesystem::canonical under the hood, if the path segment exist:

  while (PP.State != PathParser::PS_BeforeBegin) {
    tmp.assign(createView(p.native().data(), &PP.RawEntry.back()));
    error_code m_ec;
    file_status st = __status(tmp, &m_ec);
    if (!status_known(st)) {
      return err.report(m_ec);
    } else if (exists(st)) {
      result = __canonical(tmp, ec);
      break;
    }
    DNEParts.push_back(*PP);
    --PP;
  }</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>