Skip to content

llvm::sys::fs::access can spuriously fail on Windows when called with AccessMode::Exist #83046

Closed
@z2oh

Description

@z2oh

We are regularly seeing this issue surface as a compiler crash on Apple's LLVM fork: swiftlang#8224

std::error_code access(const Twine &Path, AccessMode Mode) {

I've been unable to reproduce this in isolation, but there seems to be some kind of race condition when calling GetFileAttributesW such that the call fails on a valid path with ERROR_ACCESS_DENIED. I've managed to work around this locally by avoiding the call to GetFileAttributesW when the requested access mode is AccessMode::Exist, and instead calling the shell function PathFileExistsW:

if (Mode == AccessMode::Exist) {
  if (::PathFileExistsW(PathUtf16.begin())) {
    return std::error_code();
  } else {
    return errc::no_such_file_or_directory;
  }
}

I've added some logging here and verified that this function returns correctly when GetFileAttributesW would have failed with ERROR_ACCESS_DENIED. This function requires pulling in shlwapi.h.

I'm happy to make a PR with this patch, but I wanted to make an issue first to seek alternate solutions, or to see if anyone had insight as to why GetFileAttributesW is failing in this way.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions