@samitouri / QOSAMI-WSL / commits / aae218cd

Retry deleting distribution shortcuts for 10 seconds (#41329)

Blue committed Aug 12, 2026 at 20:17 UTC aae218cd3369334b82cda8182d19d217e9cd9e59
1 file changed +16 -20
src/windows/service/exe/LxssUserSession.cpp
+16 -20
@@ -3022,6 +3022,18 @@ void LxssUserSessionImpl::_DeleteDistributionLockHeld(_In_ const LXSS_DISTRO_CON
3022 }
3023 }
3024
3025 + auto deleteWithRetry = [&](const std::filesystem::path& path) {
3026 + try
3027 + {
3028 + wsl::shared::retry::RetryWithTimeout<void>(
3029 + [&]() { THROW_IF_WIN32_BOOL_FALSE(DeleteFileW(path.c_str())); },
3030 + std::chrono::milliseconds(100),
3031 + std::chrono::seconds(10),
3032 + {HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED)});
3033 + }
3034 + CATCH_LOG_MSG("Failed to delete %ls", path.c_str())
3035 + };
3036 +
3037 // For WSL2 distributions, unmount and delete the VHD.
3038 if (WI_IsFlagSet(Flags, LXSS_DELETE_DISTRO_FLAGS_VHD) || WI_IsFlagSet(Flags, LXSS_DELETE_DISTRO_FLAGS_UNMOUNT))
3039 {
@@ -3039,15 +3051,7 @@ void LxssUserSessionImpl::_DeleteDistributionLockHeld(_In_ const LXSS_DISTRO_CON
3051 if (WI_IsFlagSet(Flags, LXSS_DELETE_DISTRO_FLAGS_VHD))
3052 {
3053 // The VHD might be in use so try to delete it for up to 10 seconds.
3042 - try
3043 - {
3044 - wsl::shared::retry::RetryWithTimeout<void>(
3045 - [&]() { THROW_IF_WIN32_BOOL_FALSE(DeleteFileW(Configuration.VhdFilePath.c_str())); },
3046 - std::chrono::milliseconds(100),
3047 - std::chrono::seconds(10),
3048 - {HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED)});
3049 - }
3050 - CATCH_LOG_MSG("Failed to delete %ls", Configuration.VhdFilePath.c_str())
3054 + deleteWithRetry(Configuration.VhdFilePath);
3055 }
3056 }
3057 }
@@ -3058,22 +3062,14 @@ void LxssUserSessionImpl::_DeleteDistributionLockHeld(_In_ const LXSS_DISTRO_CON
3062 const auto shortcutIconPath = Configuration.BasePath / c_shortIconName;
3063 if (std::filesystem::exists(shortcutIconPath))
3064 {
3061 - LOG_IF_WIN32_BOOL_FALSE_MSG(DeleteFileW(shortcutIconPath.c_str()), "Failed to delete %ls", shortcutIconPath.c_str());
3065 + deleteWithRetry(shortcutIconPath);
3066 }
3067
3068 // Remove start menu entry for the distribution, if any.
3069 if (Configuration.ShortcutPath.has_value())
3070 {
3071 // The shortcut file may be in use. Try to delete it for up to 10 seconds, and then give up.
3068 - try
3069 - {
3070 - wsl::shared::retry::RetryWithTimeout<void>(
3071 - [&]() { THROW_IF_WIN32_BOOL_FALSE(DeleteFileW(Configuration.ShortcutPath->c_str())); },
3072 - std::chrono::milliseconds(100),
3073 - std::chrono::seconds(10),
3074 - {HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION), HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED)});
3075 - }
3076 - CATCH_LOG_MSG("Failed to delete %ls", Configuration.ShortcutPath->c_str())
3072 + deleteWithRetry(Configuration.ShortcutPath.value());
3073 }
3074
3075 // Remove the terminal profile, if any.
@@ -3084,7 +3080,7 @@ void LxssUserSessionImpl::_DeleteDistributionLockHeld(_In_ const LXSS_DISTRO_CON
3080
3081 if (profile.has_value())
3082 {
3087 - LOG_IF_WIN32_BOOL_FALSE_MSG(DeleteFileW(profile->c_str()), "Failed to delete %ls", profile->c_str());
3083 + deleteWithRetry(profile.value());
3084 }
3085 }
3086 CATCH_LOG()