diff --git a/extensions/WSLExtension/Services/WslManager.cs b/extensions/WSLExtension/Services/WslManager.cs index 3f80a42911..caacab8f65 100644 --- a/extensions/WSLExtension/Services/WslManager.cs +++ b/extensions/WSLExtension/Services/WslManager.cs @@ -21,6 +21,8 @@ public class WslManager : IWslManager, IDisposable private readonly TimeSpan _oneMinutePollingInterval = TimeSpan.FromMinutes(1); + private readonly TimeSpan _packageInstallTimeOut = TimeSpan.FromMinutes(30); + private readonly WslRegisteredDistributionFactory _wslRegisteredDistributionFactory; private readonly IWslServicesMediator _wslServicesMediator; @@ -176,10 +178,7 @@ public async Task InstallDistributionPackageAsync( // Install it from the store. statusUpdateCallback?.Invoke(_stringResource.GetLocalized("DistributionPackageInstallationStart", definition.FriendlyName)); cancellationToken.ThrowIfCancellationRequested(); - if (!await _microsoftStoreService.TryInstallPackageAsync(definition.StoreAppId)) - { - throw new InvalidDataException($"Failed to install the {definition.Name} package"); - } + await _microsoftStoreService.InstallPackageAsync(definition.StoreAppId, _packageInstallTimeOut); } else { @@ -225,9 +224,10 @@ public async Task InstallWslKernelPackageAsync(Action? statusUpdateCallb { // If not installed, we'll install it from the store. statusUpdateCallback?.Invoke(_stringResource.GetLocalized("InstallingWslKernelPackage")); - cancellationToken.ThrowIfCancellationRequested(); - if (!await _microsoftStoreService.TryInstallPackageAsync(WslKernelPackageStoreId)) + await _microsoftStoreService.InstallPackageAsync(WslKernelPackageStoreId, _packageInstallTimeOut); + + if (!_packageHelper.IsPackageInstalled(WSLPackageFamilyName)) { throw new InvalidDataException("Failed to install the Wsl kernel package"); } diff --git a/services/DevHome.Services.Core/Contracts/IMicrosoftStoreService.cs b/services/DevHome.Services.Core/Contracts/IMicrosoftStoreService.cs index d3871a2a12..286821c28d 100644 --- a/services/DevHome.Services.Core/Contracts/IMicrosoftStoreService.cs +++ b/services/DevHome.Services.Core/Contracts/IMicrosoftStoreService.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +using System; using System.Threading.Tasks; using Windows.ApplicationModel.Store.Preview.InstallControl; using Windows.Foundation; @@ -37,4 +38,11 @@ public interface IMicrosoftStoreService /// Target package id /// True if package was installed, false otherwise public Task TryInstallPackageAsync(string packageId); + + /// + /// Install a package from the store. + /// + /// Target package id + /// Amount of time the operation should wait before cancelling the task. + public Task InstallPackageAsync(string packageId, TimeSpan timeout); } diff --git a/services/DevHome.Services.Core/Services/MicrosoftStoreService.cs b/services/DevHome.Services.Core/Services/MicrosoftStoreService.cs index 67fb233f68..e94942614a 100644 --- a/services/DevHome.Services.Core/Services/MicrosoftStoreService.cs +++ b/services/DevHome.Services.Core/Services/MicrosoftStoreService.cs @@ -80,20 +80,8 @@ public async Task TryInstallPackageAsync(string packageId) { try { - var installTask = InstallPackageAsync(packageId); - // Wait for a maximum of StoreInstallTimeout (60 seconds). - var completedTask = await Task.WhenAny(installTask, Task.Delay(_storeInstallTimeout)); - - if (completedTask.Exception != null) - { - throw completedTask.Exception; - } - - if (completedTask != installTask) - { - throw new TimeoutException("Store Install task did not finish in time."); - } + await InstallPackageAsync(packageId, _storeInstallTimeout); return true; } @@ -105,6 +93,23 @@ public async Task TryInstallPackageAsync(string packageId) return false; } + public async Task InstallPackageAsync(string packageId, TimeSpan timeout) + { + var installTask = InstallPackageAsync(packageId); + + var completedTask = await Task.WhenAny(installTask, Task.Delay(timeout)); + + if (completedTask.Exception != null) + { + throw completedTask.Exception; + } + + if (completedTask != installTask) + { + throw new TimeoutException("Store Install task did not finish in time."); + } + } + private async Task InstallPackageAsync(string packageId) { await Task.Run(() =>