Skip to content

Commit 9c5b7e9

Browse files
authored
Fix random "ERROR_FILE_NOT_FOUND" when unmounting with absolute path (#40092)
Fix ERROR_FILE_NOT_FOUND when unmounting a vhd with absolute path after the vm timeouts.
1 parent 1a2ede7 commit 9c5b7e9

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/windows/common/WslClient.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,9 +1218,8 @@ int Unmount(_In_ const std::wstring& arg)
12181218
wsl::windows::common::SvcComm service;
12191219
const HRESULT result = wil::ResultFromException([&] { value = service.DetachDisk(disk); });
12201220

1221-
// support relative paths in unmount
1222-
// check is the result is the error code for "file not found" and the path is relative
1223-
if (result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) && PathIsRelative(disk))
1221+
// Retry with the normalized path to handle relative paths and \\?\ prefix mismatches.
1222+
if (result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
12241223
{
12251224
// retry dismounting with the absolute path
12261225
const auto absoluteDisk = wsl::windows::common::filesystem::GetFullPath(filesystem::UnquotePath(disk).c_str());

test/windows/MountTests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,29 @@ class MountTests
450450
VERIFY_ARE_EQUAL(LxsstuLaunchWsl(L"--unmount " + absolutePath.wstring()), (DWORD)0);
451451
}
452452

453+
TEST_METHOD(AbsolutePathVhdUnmountAfterVMTimeout)
454+
{
455+
SKIP_UNSUPPORTED_ARM64_MOUNT_TEST();
456+
WSL2_TEST_ONLY();
457+
458+
WslKeepAlive keepAlive;
459+
460+
VERIFY_ARE_EQUAL(LxsstuLaunchWsl(L"--mount " TEST_MOUNT_VHD L" --vhd --bare"), (DWORD)0);
461+
462+
const auto disk = GetBlockDeviceInWsl();
463+
VERIFY_IS_TRUE(IsBlockDevicePresent(disk));
464+
465+
WaitForVmTimeout(keepAlive);
466+
467+
const auto absolutePath = std::filesystem::absolute(TEST_MOUNT_VHD);
468+
469+
// Validate that the vhd path doesn't start with '\\?'
470+
VERIFY_IS_FALSE(absolutePath.wstring().starts_with(L"\\"));
471+
472+
// Validate the unmounting by absolute path is successful
473+
VERIFY_ARE_EQUAL(LxsstuLaunchWsl(L"--unmount " + absolutePath.wstring()), (DWORD)0);
474+
}
475+
453476
// Attach a disk, but don't mount it
454477
TEST_METHOD(TestBareMount)
455478
{

0 commit comments

Comments
 (0)