@samitouri / QOSAMI-WSL / commits / 407bfda0

Don't throw when unregistering a distro that has a BasePath that doesn't exist (#13130)

Blue committed Jun 17, 2025 at 15:23 UTC 407bfda0994e6b00fb1044d7d6dbdf97d062ff1c
2 files changed +31 -3
src/windows/service/exe/LxssUserSession.cpp
+7 -3
@@ -2937,11 +2937,15 @@ void LxssUserSessionImpl::_DeleteDistributionLockHeld(_In_ const LXSS_DISTRO_CON
2937 CATCH_LOG()
2938
2939 // If the basepath is empty, delete it.
2940 - if (std::filesystem::is_empty(Configuration.BasePath))
2940 + try
2941 {
2942 - LOG_IF_WIN32_BOOL_FALSE_MSG(
2943 - RemoveDirectory(Configuration.BasePath.c_str()), "Failed to delete %ls", Configuration.BasePath.c_str());
2942 + if (std::filesystem::is_empty(Configuration.BasePath))
2943 + {
2944 + LOG_IF_WIN32_BOOL_FALSE_MSG(
2945 + RemoveDirectory(Configuration.BasePath.c_str()), "Failed to delete %ls", Configuration.BasePath.c_str());
2946 + }
2947 }
2948 + CATCH_LOG();
2949 }
2950
2951 _Requires_exclusive_lock_held_(m_instanceLock)
test/windows/UnitTests.cpp
+24
@@ -6095,5 +6095,29 @@ Error code: Wsl/InstallDistro/WSL_E_INVALID_JSON\r\n",
6095 "#Comment 127.0.0.1 microsoft.com windows.microsoft.com\n#AnotherComment\n127.0.0.1 wsl.dev", "127.0.0.1\twsl.dev\n");
6096 }
6097
6098 + // Validate that a distribution can be unregistered even if its BasePath doesn't exist.
6099 + // See https://github.com/microsoft/WSL/issues/13004
6100 + TEST_METHOD(BrokenDistroUnregister)
6101 + {
6102 + const auto userKey = wsl::windows::common::registry::OpenLxssUserKey();
6103 + const auto distroKey = wsl::windows::common::registry::CreateKey(userKey.get(), L"{baa405ef-1822-4bbe-84e2-30e4c6330d42}");
6104 +
6105 + auto revert = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&] {
6106 + wsl::windows::common::registry::DeleteKey(userKey.get(), L"{baa405ef-1822-4bbe-84e2-30e4c6330d42}");
6107 + });
6108 +
6109 + wsl::windows::common::registry::WriteString(distroKey.get(), nullptr, L"BasePath", L"C:\\DoesNotExit");
6110 + wsl::windows::common::registry::WriteString(distroKey.get(), nullptr, L"DistributionName", L"DummyBrokenDistro");
6111 + wsl::windows::common::registry::WriteDword(distroKey.get(), nullptr, L"DefaultUid", 0);
6112 + wsl::windows::common::registry::WriteDword(distroKey.get(), nullptr, L"Version", LXSS_DISTRO_VERSION_2);
6113 + wsl::windows::common::registry::WriteDword(distroKey.get(), nullptr, L"State", LxssDistributionStateInstalled);
6114 + wsl::windows::common::registry::WriteDword(distroKey.get(), nullptr, L"Flags", LXSS_DISTRO_FLAGS_VM_MODE);
6115 +
6116 + auto [out, err] = LxsstuLaunchWslAndCaptureOutput(L"--unregister DummyBrokenDistro");
6117 +
6118 + VERIFY_ARE_EQUAL(out, L"The operation completed successfully. \r\n");
6119 + VERIFY_ARE_EQUAL(err, L"");
6120 + }
6121 +
6122 }; // namespace UnitTests
6123 } // namespace UnitTests