From c03fc79858eb636f264f8e37d451d63f360adbeb Mon Sep 17 00:00:00 2001 From: Jean-Pierre Briede Date: Wed, 27 Jul 2022 15:23:35 -0700 Subject: [PATCH 1/2] Fix a code path that calls into CreateCacheFileAsync from running on the UI thread. --- .../Xamls/PackageManagerControl.xaml.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.UI/Xamls/PackageManagerControl.xaml.cs b/src/NuGet.Clients/NuGet.PackageManagement.UI/Xamls/PackageManagerControl.xaml.cs index b03175d5e96..27ad7dbbcf3 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.UI/Xamls/PackageManagerControl.xaml.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.UI/Xamls/PackageManagerControl.xaml.cs @@ -950,13 +950,16 @@ private async ValueTask RefreshInstalledAndUpdatesTabsAsync() private async Task<(int, int)> GetInstalledVulnerableAndDeprecatedPackagesCountAsync(PackageLoadContext loadContext, CancellationToken token) { + await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + IReadOnlyCollection packageSources = SelectedSource.PackageSources; + // Switch off the UI thread before fetching installed packages and deprecation metadata. await TaskScheduler.Default; PackageCollection installedPackages = await loadContext.GetInstalledPackagesAsync(); var installedPackageMetadata = await Task.WhenAll( - installedPackages.Select(p => GetPackageMetadataAsync(p, token))); + installedPackages.Select(p => GetPackageMetadataAsync(p, packageSources, token))); int vulnerablePackagesCount = 0; int deprecatedPackagesCount = 0; @@ -975,13 +978,12 @@ private async ValueTask RefreshInstalledAndUpdatesTabsAsync() return (vulnerablePackagesCount, deprecatedPackagesCount); } - private async Task<(PackageSearchMetadataContextInfo, PackageDeprecationMetadataContextInfo)> GetPackageMetadataAsync(PackageCollectionItem package, CancellationToken cancellationToken) + private async Task<(PackageSearchMetadataContextInfo, PackageDeprecationMetadataContextInfo)> GetPackageMetadataAsync(PackageCollectionItem package, IReadOnlyCollection packageSources, CancellationToken cancellationToken) { using (INuGetSearchService searchService = await _serviceBroker.GetProxyAsync(NuGetServices.SearchService, cancellationToken: cancellationToken)) { Assumes.NotNull(searchService); - await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); - return await searchService.GetPackageMetadataAsync(package, SelectedSource.PackageSources, true, cancellationToken); + return await searchService.GetPackageMetadataAsync(package, packageSources, true, cancellationToken); } } From e9ffc6777756c7fd72f6de20b594d9ce9566e44f Mon Sep 17 00:00:00 2001 From: Jean-Pierre Briede Date: Thu, 28 Jul 2022 18:03:04 -0700 Subject: [PATCH 2/2] Address PR feedback --- .../Xamls/PackageManagerControl.xaml.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/NuGet.Clients/NuGet.PackageManagement.UI/Xamls/PackageManagerControl.xaml.cs b/src/NuGet.Clients/NuGet.PackageManagement.UI/Xamls/PackageManagerControl.xaml.cs index 27ad7dbbcf3..89712ac4134 100644 --- a/src/NuGet.Clients/NuGet.PackageManagement.UI/Xamls/PackageManagerControl.xaml.cs +++ b/src/NuGet.Clients/NuGet.PackageManagement.UI/Xamls/PackageManagerControl.xaml.cs @@ -934,7 +934,7 @@ private async ValueTask RefreshInstalledAndUpdatesTabsAsync() Interlocked.Exchange(ref _refreshCts, refreshCts)?.Cancel(); // Update installed tab warning icon - (int vulnerablePackages, int deprecatedPackages) = await GetInstalledVulnerableAndDeprecatedPackagesCountAsync(loadContext, refreshCts.Token); + (int vulnerablePackages, int deprecatedPackages) = await GetInstalledVulnerableAndDeprecatedPackagesCountAsync(loadContext, SelectedSource.PackageSources, refreshCts.Token); _topPanel.UpdateWarningStatusOnInstalledTab(vulnerablePackages, deprecatedPackages); // Update updates tab count @@ -948,11 +948,8 @@ private async ValueTask RefreshInstalledAndUpdatesTabsAsync() _topPanel.UpdateCountOnUpdatesTab(Model.CachedUpdates.Packages.Count); } - private async Task<(int, int)> GetInstalledVulnerableAndDeprecatedPackagesCountAsync(PackageLoadContext loadContext, CancellationToken token) + private async Task<(int, int)> GetInstalledVulnerableAndDeprecatedPackagesCountAsync(PackageLoadContext loadContext, IReadOnlyCollection packageSources, CancellationToken token) { - await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); - IReadOnlyCollection packageSources = SelectedSource.PackageSources; - // Switch off the UI thread before fetching installed packages and deprecation metadata. await TaskScheduler.Default;