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.
Feng Wang committed
Apr 10, 2026 at 11:17 UTC
9c5b7e988a83a41534c2e3a6d726b19fef1b423a
2 files changed
+25
-3
src/windows/common/WslClient.cpp
+2
-3
@@ -1218,9 +1218,8 @@ int Unmount(_In_ const std::wstring& arg)
1218
wsl::windows::common::SvcComm service;
1219
const HRESULT result = wil::ResultFromException([&] { value = service.DetachDisk(disk); });
1220
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))
1223
{
1224
// retry dismounting with the absolute path
1225
const auto absoluteDisk = wsl::windows::common::filesystem::GetFullPath(filesystem::UnquotePath(disk).c_str());
test/windows/MountTests.cpp
+23
@@ -450,6 +450,29 @@ class MountTests
450
VERIFY_ARE_EQUAL(LxsstuLaunchWsl(L"--unmount " + absolutePath.wstring()), (DWORD)0);
451
}
452
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
+
476
// Attach a disk, but don't mount it
477
TEST_METHOD(TestBareMount)
478
{