From 407bfda0994e6b00fb1044d7d6dbdf97d062ff1c Mon Sep 17 00:00:00 2001 From: Blue Date: Tue, 17 Jun 2025 15:23:31 -0700 Subject: [PATCH] Don't throw when unregistering a distro that has a BasePath that doesn't exist (#13130) --- src/windows/service/exe/LxssUserSession.cpp | 10 ++++++--- test/windows/UnitTests.cpp | 24 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/windows/service/exe/LxssUserSession.cpp b/src/windows/service/exe/LxssUserSession.cpp index 468273f..4de6ce8 100644 --- a/src/windows/service/exe/LxssUserSession.cpp +++ b/src/windows/service/exe/LxssUserSession.cpp @@ -2937,11 +2937,15 @@ void LxssUserSessionImpl::_DeleteDistributionLockHeld(_In_ const LXSS_DISTRO_CON CATCH_LOG() // If the basepath is empty, delete it. - if (std::filesystem::is_empty(Configuration.BasePath)) + try { - LOG_IF_WIN32_BOOL_FALSE_MSG( - RemoveDirectory(Configuration.BasePath.c_str()), "Failed to delete %ls", Configuration.BasePath.c_str()); + if (std::filesystem::is_empty(Configuration.BasePath)) + { + LOG_IF_WIN32_BOOL_FALSE_MSG( + RemoveDirectory(Configuration.BasePath.c_str()), "Failed to delete %ls", Configuration.BasePath.c_str()); + } } + CATCH_LOG(); } _Requires_exclusive_lock_held_(m_instanceLock) diff --git a/test/windows/UnitTests.cpp b/test/windows/UnitTests.cpp index f4e2c4a..8a12a25 100644 --- a/test/windows/UnitTests.cpp +++ b/test/windows/UnitTests.cpp @@ -6095,5 +6095,29 @@ Error code: Wsl/InstallDistro/WSL_E_INVALID_JSON\r\n", "#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"); } + // Validate that a distribution can be unregistered even if its BasePath doesn't exist. + // See https://github.com/microsoft/WSL/issues/13004 + TEST_METHOD(BrokenDistroUnregister) + { + const auto userKey = wsl::windows::common::registry::OpenLxssUserKey(); + const auto distroKey = wsl::windows::common::registry::CreateKey(userKey.get(), L"{baa405ef-1822-4bbe-84e2-30e4c6330d42}"); + + auto revert = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&] { + wsl::windows::common::registry::DeleteKey(userKey.get(), L"{baa405ef-1822-4bbe-84e2-30e4c6330d42}"); + }); + + wsl::windows::common::registry::WriteString(distroKey.get(), nullptr, L"BasePath", L"C:\\DoesNotExit"); + wsl::windows::common::registry::WriteString(distroKey.get(), nullptr, L"DistributionName", L"DummyBrokenDistro"); + wsl::windows::common::registry::WriteDword(distroKey.get(), nullptr, L"DefaultUid", 0); + wsl::windows::common::registry::WriteDword(distroKey.get(), nullptr, L"Version", LXSS_DISTRO_VERSION_2); + wsl::windows::common::registry::WriteDword(distroKey.get(), nullptr, L"State", LxssDistributionStateInstalled); + wsl::windows::common::registry::WriteDword(distroKey.get(), nullptr, L"Flags", LXSS_DISTRO_FLAGS_VM_MODE); + + auto [out, err] = LxsstuLaunchWslAndCaptureOutput(L"--unregister DummyBrokenDistro"); + + VERIFY_ARE_EQUAL(out, L"The operation completed successfully. \r\n"); + VERIFY_ARE_EQUAL(err, L""); + } + }; // namespace UnitTests } // namespace UnitTests