Skip to content

Commit e5d5f14

Browse files
baronfelCopilot
andcommitted
Preserve published tool packages when metadata publication fails
Delete failed extractions only while neither completion marker exists. Once published, retain the archive and extracted files so concurrent installs remain valid and NuGet can repair missing metadata under its canonical lock. Cover failures before and after the metadata move with concurrent cache reuse and verify that both cases fail before the cleanup fix. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 7349d77 commit e5d5f14

2 files changed

Lines changed: 153 additions & 5 deletions

File tree

‎src/Cli/dotnet/ToolPackage/ToolPackageDownloader.cs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ protected override NuGetVersion DownloadAndExtractPackage(
124124
}
125125
catch
126126
{
127-
// If something fails, don't leave a folder with partial contents.
128-
if (Directory.Exists(packageDirectory))
127+
// A completion marker can expose the package to other installs. Preserve it so NuGet can repair missing metadata.
128+
if (!File.Exists(hashPath) && !File.Exists(metadataPath) && Directory.Exists(packageDirectory))
129129
{
130130
Directory.Delete(packageDirectory, true);
131131
}
@@ -171,7 +171,7 @@ protected virtual void MovePackageHash(string temporaryHashPath, string hashPath
171171
/// Publishes NuGet's canonical completion marker so other NuGet writers preserve the completed package.
172172
/// </summary>
173173
/// <remarks>The metadata is written to a sibling temporary file and moved last while the NuGet package lock is held.</remarks>
174-
private static void PublishPackageMetadata(string metadataPath, string contentHash)
174+
private void PublishPackageMetadata(string metadataPath, string contentHash)
175175
{
176176
string metadataDirectory = Path.GetDirectoryName(metadataPath)!;
177177
Directory.CreateDirectory(metadataDirectory);
@@ -185,14 +185,19 @@ private static void PublishPackageMetadata(string metadataPath, string contentHa
185185
{
186186
ContentHash = contentHash
187187
});
188-
File.Move(temporaryMetadataPath, metadataPath, overwrite: true);
188+
MovePackageMetadata(temporaryMetadataPath, metadataPath);
189189
}
190190
finally
191191
{
192192
File.Delete(temporaryMetadataPath);
193193
}
194194
}
195195

196+
protected virtual void MovePackageMetadata(string temporaryMetadataPath, string metadataPath)
197+
{
198+
File.Move(temporaryMetadataPath, metadataPath, overwrite: true);
199+
}
200+
196201
protected override bool IsPackageInstalled(PackageId packageId, NuGetVersion packageVersion, string packagesRootPath)
197202
{
198203
NuGetv3LocalRepository nugetLocalRepository = new(packagesRootPath);

‎test/Microsoft.DotNet.PackageInstall.Tests/ToolPackageDownloaderTests.cs‎

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,110 @@ public async Task GivenPackageHashPublicationInProgressThePackageIsNotReportedAs
869869
NupkgMetadataFileFormat.Read(metadataPath).ContentHash.Should().Be(expectedContentHash);
870870
}
871871

872+
[Theory]
873+
[InlineData(false)]
874+
[InlineData(true)]
875+
public async Task GivenMetadataPublicationFailsItPreservesThePackageForAnotherInstall(bool metadataPublished)
876+
{
877+
string source = GetTestLocalFeedPath();
878+
var root = _testAssetsManager.CreateTestDirectory(identifier: metadataPublished.ToString());
879+
var packagesRoot = new DirectoryPath(Path.Combine(root.Path, "packages"));
880+
string runtimeJsonPath = Path.Combine(
881+
SdkTestContext.Current.ToolsetUnderTest.SdkFolderUnderTest,
882+
"RuntimeIdentifierGraph.json");
883+
var packageVersion = NuGetVersion.Parse(TestPackageVersion);
884+
var packageSourceLocation = new PackageSourceLocation(additionalSourceFeeds: [source], basePath: root.Path);
885+
var pathResolver = new VersionFolderPathResolver(packagesRoot.Value);
886+
string hashPath = pathResolver.GetHashPath(TestPackageId.ToString(), packageVersion);
887+
string metadataPath = pathResolver.GetNupkgMetadataPath(TestPackageId.ToString(), packageVersion);
888+
var metadataPublicationReached = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
889+
using var continuePublication = new ManualResetEventSlim(initialState: false);
890+
Action failPublication = () =>
891+
{
892+
metadataPublicationReached.SetResult();
893+
continuePublication.Wait();
894+
throw new IOException("Simulated metadata publication failure.");
895+
};
896+
using var downloader = new TestableToolPackageDownloader(
897+
new ToolPackageStoreAndQuery(new DirectoryPath(Path.Combine(root.Path, "tools"))),
898+
runtimeJsonPath,
899+
root.Path,
900+
beforeMetadataPublication: metadataPublished ? null : failPublication,
901+
afterMetadataPublication: metadataPublished ? failPublication : null);
902+
using var reusingDownloader = new TestableToolPackageDownloader(
903+
new ToolPackageStoreAndQuery(new DirectoryPath(Path.Combine(root.Path, "reusing-tools"))),
904+
runtimeJsonPath,
905+
root.Path);
906+
907+
Task<NuGetVersion> download = Task.Run(() =>
908+
{
909+
using FileStream installLock = ToolPackageDownloaderBase.AcquireToolInstallLock(
910+
packagesRoot, TestPackageId, packageVersion);
911+
return downloader.DownloadAndExtract(
912+
TestPackageId,
913+
downloader.CreatePackageDownloader(),
914+
packagesRoot.Value,
915+
packageVersion,
916+
packageSourceLocation);
917+
});
918+
919+
Task<IToolPackage> reuse;
920+
string publishedHash;
921+
try
922+
{
923+
Task firstCompleted = await Task.WhenAny(metadataPublicationReached.Task, download)
924+
.WaitAsync(TimeSpan.FromSeconds(30));
925+
firstCompleted.Should().Be(metadataPublicationReached.Task);
926+
publishedHash = File.ReadAllText(hashPath);
927+
File.Exists(metadataPath).Should().Be(metadataPublished);
928+
reuse = Task.Run(() => reusingDownloader.InstallFromCache(
929+
TestPackageId,
930+
packageVersion,
931+
packagesRoot,
932+
packageSourceLocation,
933+
_testTargetframework));
934+
if (metadataPublished)
935+
{
936+
IToolPackage acceptedPackage = await reuse.WaitAsync(TimeSpan.FromSeconds(30));
937+
File.Exists(acceptedPackage.Command.Executable.Value).Should().BeTrue();
938+
}
939+
else
940+
{
941+
(await Task.WhenAny(reuse, Task.Delay(TimeSpan.FromMilliseconds(250))))
942+
.Should().NotBe(reuse, "NuGet must wait for the writer before it can repair the missing metadata");
943+
}
944+
}
945+
finally
946+
{
947+
continuePublication.Set();
948+
}
949+
950+
Func<Task> awaitDownload = async () => await download;
951+
await awaitDownload.Should().ThrowAsync<IOException>()
952+
.WithMessage("Simulated metadata publication failure.");
953+
IToolPackage reusedPackage = await reuse.WaitAsync(TimeSpan.FromSeconds(30));
954+
File.Exists(reusedPackage.Command.Executable.Value).Should().BeTrue(
955+
"metadata publication failure must not delete a package accepted by another install");
956+
File.ReadAllText(hashPath).Should().Be(publishedHash);
957+
File.Exists(metadataPath).Should().BeTrue(
958+
"NuGet must preserve published metadata or repair it after the writer releases its lock");
959+
var packageDownloader = new StaticPackagePathNuGetPackageDownloader("must-not-download");
960+
reusingDownloader.DownloadAndExtract(
961+
TestPackageId,
962+
packageDownloader,
963+
packagesRoot.Value,
964+
packageVersion,
965+
packageSourceLocation).Should().Be(packageVersion);
966+
packageDownloader.ExtractCalled.Should().BeFalse();
967+
File.ReadAllText(hashPath).Should().Be(publishedHash);
968+
File.Exists(reusedPackage.Command.Executable.Value).Should().BeTrue();
969+
using FileStream packageStream = File.OpenRead(
970+
pathResolver.GetPackageFilePath(TestPackageId.ToString(), packageVersion));
971+
using PackageArchiveReader reader = new(packageStream);
972+
NupkgMetadataFileFormat.Read(metadataPath).ContentHash.Should().Be(
973+
reader.GetContentHash(CancellationToken.None, () => publishedHash));
974+
}
975+
872976
[Fact]
873977
public async Task GivenNuGetsPackageLockToolInstallationWaitsForTheCanonicalWriter()
874978
{
@@ -1451,6 +1555,8 @@ private static string GetTestLocalFeedPath() =>
14511555
private sealed class TestableToolPackageDownloader : ToolPackageDownloader, IDisposable
14521556
{
14531557
private readonly bool _pauseHashPublication;
1558+
private readonly Action _beforeMetadataPublication;
1559+
private readonly Action _afterMetadataPublication;
14541560
private readonly TaskCompletionSource _hashMoveStarted =
14551561
new(TaskCreationOptions.RunContinuationsAsynchronously);
14561562
private readonly ManualResetEventSlim _continueHashMove = new(initialState: false);
@@ -1459,10 +1565,14 @@ public TestableToolPackageDownloader(
14591565
IToolPackageStore store,
14601566
string runtimeJsonPathForTests,
14611567
string currentWorkingDirectory,
1462-
bool pauseHashPublication = false)
1568+
bool pauseHashPublication = false,
1569+
Action beforeMetadataPublication = null,
1570+
Action afterMetadataPublication = null)
14631571
: base(store, runtimeJsonPathForTests, currentWorkingDirectory)
14641572
{
14651573
_pauseHashPublication = pauseHashPublication;
1574+
_beforeMetadataPublication = beforeMetadataPublication;
1575+
_afterMetadataPublication = afterMetadataPublication;
14661576
}
14671577

14681578
public Task HashMoveStarted => _hashMoveStarted.Task;
@@ -1490,6 +1600,32 @@ public NuGetVersion DownloadAndExtract(
14901600
public bool IsInstalled(PackageId packageId, NuGetVersion packageVersion, string packagesRootPath) =>
14911601
IsPackageInstalled(packageId, packageVersion, packagesRootPath);
14921602

1603+
public IToolPackage InstallFromCache(
1604+
PackageId packageId,
1605+
NuGetVersion packageVersion,
1606+
DirectoryPath packagesRoot,
1607+
PackageSourceLocation packageSourceLocation,
1608+
string targetFramework)
1609+
{
1610+
DownloadTool(
1611+
packagesRoot,
1612+
packageId,
1613+
packageVersion,
1614+
CreatePackageDownloader(),
1615+
packageSourceLocation,
1616+
givenSpecificVersion: true,
1617+
assetFileDirectory: _localToolAssetDir,
1618+
acquireInstallLock: true,
1619+
targetFramework,
1620+
TestVerbosity);
1621+
return new ToolPackageInstance(
1622+
packageId,
1623+
packageVersion,
1624+
packagesRoot,
1625+
_localToolAssetDir,
1626+
_fileSystem);
1627+
}
1628+
14931629
public void ContinueHashMove() => _continueHashMove.Set();
14941630

14951631
protected override void MovePackageHash(string temporaryHashPath, string hashPath)
@@ -1503,6 +1639,13 @@ protected override void MovePackageHash(string temporaryHashPath, string hashPat
15031639
base.MovePackageHash(temporaryHashPath, hashPath);
15041640
}
15051641

1642+
protected override void MovePackageMetadata(string temporaryMetadataPath, string metadataPath)
1643+
{
1644+
_beforeMetadataPublication?.Invoke();
1645+
base.MovePackageMetadata(temporaryMetadataPath, metadataPath);
1646+
_afterMetadataPublication?.Invoke();
1647+
}
1648+
15061649
public void Dispose() => _continueHashMove.Dispose();
15071650
}
15081651

0 commit comments

Comments
 (0)