Skip to content

Fix random "ERROR_FILE_NOT_FOUND" when unmounting with absolute path#40092

Open
chemwolf6922 wants to merge 1 commit intomasterfrom
user/chemwolf6922/fix-unmount-file-not-found
Open

Fix random "ERROR_FILE_NOT_FOUND" when unmounting with absolute path#40092
chemwolf6922 wants to merge 1 commit intomasterfrom
user/chemwolf6922/fix-unmount-file-not-found

Conversation

@chemwolf6922
Copy link
Copy Markdown
Contributor

Summary of the Pull Request

Fix this random issue where sometimes unmount with the absolute path results in ERROR_FILE_NOT_FOUND

PS D:\tmp> wsl --mount "D:\tmp\test.vhdx" --vhd --bare
wsl: The .wslconfig setting 'wsl2.kernelCommandLine' is disabled by the computer policy.
The operation completed successfully.
PS D:\tmp> wsl --unmount  "D:\tmp\test.vhdx"
The system cannot find the file specified.
Error code: Wsl/Service/DetachDisk/ERROR_FILE_NOT_FOUND
PS D:\tmp> wsl --unmount  "\\?\D:\tmp\test.vhdx"
The operation completed successfully.

PR Checklist

  • Closes: Link to issue #xxx
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

There is already a test case MountTests::MountTests::AbsolutePathVhdUnmount for this. But since this issue is not deterministic, it does not catch the problem reliably.
Instead, I did this loop for 20 times for before and after the fix:

wsl --shutdown
wsl --mount "D:\tmp\test.vhdx" --vhd --bare
wsl --unmount  "D:\tmp\test.vhdx"
# And wsl --unmount  "\\?\D:\tmp\test.vhdx" if it fails.

Before:
6 / 20 hits.
After:
0 / 20 hits.

Copilot AI review requested due to automatic review settings April 3, 2026 06:48
@chemwolf6922 chemwolf6922 requested a review from a team as a code owner April 3, 2026 06:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses intermittent ERROR_FILE_NOT_FOUND failures when unmounting a VHD by an absolute path without the \\?\ prefix, by retrying the detach using a normalized path representation.

Changes:

  • Retry DetachDisk with a normalized/full path on ERROR_FILE_NOT_FOUND (not only for relative paths), to handle \\?\ prefix mismatches.
  • Update the inline comment to reflect the broadened retry behavior.
Comments suppressed due to low confidence (1)

src/windows/common/WslClient.cpp:1226

  • The unconditional retry normalizes the disk argument via filesystem::GetFullPath(), which only works for real filesystem paths. If the user passes a non-filesystem disk identifier (e.g. passthrough paths like "\.\PHYSICALDRIVE…") and DetachDisk returns ERROR_FILE_NOT_FOUND, GetFullPath will throw (often ERROR_INVALID_NAME), changing the user-visible failure from ERROR_FILE_NOT_FOUND to a different error. Consider making normalization best-effort (catch/ignore GetFullPath failures and fall back to the original HRESULT), or gate the retry to VHD/filesystem-style paths only.
    // Retry with the normalized path to handle relative paths and \\?\ prefix mismatches.
    if (result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
    {
        // retry dismounting with the absolute path
        const auto absoluteDisk = wsl::windows::common::filesystem::GetFullPath(filesystem::UnquotePath(disk).c_str());
        value = service.DetachDisk(absoluteDisk.c_str());

// check is the result is the error code for "file not found" and the path is relative
if (result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) && PathIsRelative(disk))
// Retry with the normalized path to handle relative paths and \\?\ prefix mismatches.
if (result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you figure out why the result is not deterministic? That’s very odd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants