fix(launcher): harden installer transactions

This commit is contained in:
Erik 2026-08-14 20:36:11 +02:00
parent ff6ebb6a6a
commit 3f68895120
21 changed files with 1164 additions and 61 deletions

View file

@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Text.Json.Nodes;
using AcDream.Launcher.Core.Integrity;
using AcDream.Launcher.Core.Installation;
@ -168,6 +169,33 @@ public sealed class LauncherInstallerTests : IDisposable
Assert.Equal(LauncherInstallPhase.Failed, progress[^1].Phase);
}
[Fact]
public async Task ContradictoryTerminalCannotReplaceFirstFailureOrPriorInstall()
{
(LauncherInstaller installer, LauncherInstallRecordStore store, LauncherInstallRecord old) =
await CreateInstallerWithPriorRecordAsync(
async (request, output, _) =>
{
await File.WriteAllTextAsync(request.OutputPath, "contradictory output");
long bytes = new FileInfo(request.OutputPath).Length;
output("{\"v\":1,\"e\":\"started\",\"bakeToolVersion\":4}\n");
output("{\"v\":1,\"e\":\"error\",\"message\":\"first failure\"}\n");
output($"{{\"v\":1,\"e\":\"completed\",\"bakeToolVersion\":4,"
+ $"\"outputBytes\":{bytes},\"failures\":0}}\n");
return new BakeProcessResult(0, string.Empty);
});
LauncherInstallException exception =
await Assert.ThrowsAsync<LauncherInstallException>(
() => installer.InstallAsync(_dats, 2));
Assert.Contains("after", exception.Message, StringComparison.OrdinalIgnoreCase);
Assert.Equal(
"previous verified package",
await File.ReadAllTextAsync(store.PreparedAssetPath));
Assert.Equal(old, (await store.LoadAndVerifyAsync()).Record);
}
[Fact]
public async Task CancellationRestoresPriorInstallAndNeverPublishesPartialOutput()
{
@ -270,6 +298,187 @@ public sealed class LauncherInstallerTests : IDisposable
Assert.False(File.Exists(store.RecordPath));
}
[Fact]
public async Task IndependentInstallersSerializeAndWaitingCancellationTouchesNothing()
{
var storeA = new LauncherInstallRecordStore(_paths);
LauncherInstallRecord old = await CreatePriorRecordAsync(storeA);
string backupPath = LauncherInstallRecordStore.GetBackupPath(
storeA.PreparedAssetPath);
var childEntered = new TaskCompletionSource(
TaskCreationOptions.RunContinuationsAsynchronously);
var releaseChild = new TaskCompletionSource(
TaskCreationOptions.RunContinuationsAsynchronously);
var runnerA = new FakeBakeProcessRunner(async (request, _, _) =>
{
await File.WriteAllTextAsync(request.OutputPath, "installer A in progress");
childEntered.SetResult();
await releaseChild.Task;
return new BakeProcessResult(1, "fixture A failed");
});
bool runnerBEntered = false;
var runnerB = new FakeBakeProcessRunner((_, _, _) =>
{
runnerBEntered = true;
return Task.FromResult(new BakeProcessResult(1, "must not run"));
});
var installerA = new LauncherInstaller(
_paths,
_bakeExecutable,
recordStore: storeA,
processRunner: runnerA);
var installerB = new LauncherInstaller(
_paths,
_bakeExecutable,
recordStore: new LauncherInstallRecordStore(_paths),
processRunner: runnerB);
Task<LauncherInstallResult> operationA =
installerA.InstallAsync(_dats, 1);
await childEntered.Task.WaitAsync(TimeSpan.FromSeconds(5));
try
{
using var cancellationB = new CancellationTokenSource();
Task<LauncherInstallResult> operationB = installerB.InstallAsync(
_dats,
1,
cancellationToken: cancellationB.Token);
await Task.Delay(150);
cancellationB.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => operationB);
Assert.False(runnerBEntered);
Assert.Equal(
"installer A in progress",
await File.ReadAllTextAsync(storeA.PreparedAssetPath));
Assert.Equal(
"previous verified package",
await File.ReadAllTextAsync(backupPath));
}
finally
{
releaseChild.TrySetResult();
}
await Assert.ThrowsAsync<LauncherInstallException>(() => operationA);
Assert.Equal(
"previous verified package",
await File.ReadAllTextAsync(storeA.PreparedAssetPath));
Assert.False(File.Exists(backupPath));
Assert.Equal(old, (await storeA.LoadAndVerifyAsync()).Record);
}
[Fact]
public void StagingCleanupDeletesOnlyExactBakeTransactionNames()
{
var store = new LauncherInstallRecordStore(_paths);
string outputPath = store.PreparedAssetPath;
string directory = Path.GetDirectoryName(outputPath)!;
Directory.CreateDirectory(directory);
string owned = BakeOutputStagingContract.CreateStagingPath(
outputPath,
Guid.Parse("01234567-89ab-cdef-0123-456789abcdef"));
string canonical = outputPath;
string backup = LauncherInstallRecordStore.GetBackupPath(outputPath);
string oldPattern = Path.Combine(
directory,
$".{Path.GetFileName(outputPath)}.{Guid.NewGuid():N}.tmp");
string invalidTransaction = Path.Combine(
directory,
$".{Path.GetFileName(outputPath)}.acdream-bake.not-a-guid.tmp");
string unrelated = Path.Combine(directory, "unrelated.tmp");
Assert.Equal(
Path.Combine(
directory,
".acdream.pak.acdream-bake.0123456789abcdef0123456789abcdef.tmp"),
owned);
foreach (string path in new[]
{
owned,
canonical,
backup,
oldPattern,
invalidTransaction,
unrelated,
})
{
File.WriteAllText(path, Path.GetFileName(path));
}
BakeOutputStagingContract.DeleteOwnedStagingFiles(outputPath);
Assert.False(File.Exists(owned));
Assert.True(File.Exists(canonical));
Assert.True(File.Exists(backup));
Assert.True(File.Exists(oldPattern));
Assert.True(File.Exists(invalidTransaction));
Assert.True(File.Exists(unrelated));
}
[Fact]
public async Task KilledProcessReleasesLeaseAndRestartReclaimsOnlyBakeStaging()
{
var store = new LauncherInstallRecordStore(_paths);
LauncherInstallRecord old = await CreatePriorRecordAsync(store);
string staging = BakeOutputStagingContract.CreateStagingPath(
store.PreparedAssetPath,
Guid.Parse("fedcba98-7654-3210-fedc-ba9876543210"));
string ready = Path.Combine(_root, "fixture-ready");
string fixtureDll = GetInstallLeaseFixturePath();
Assert.True(File.Exists(fixtureDll), $"Missing fixture: {fixtureDll}");
var startInfo = new ProcessStartInfo("dotnet")
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
};
startInfo.ArgumentList.Add(fixtureDll);
startInfo.ArgumentList.Add(
InstallerTransactionLease.GetLockPath(store.DataDirectory));
startInfo.ArgumentList.Add(staging);
startInfo.ArgumentList.Add(ready);
using Process helper = Process.Start(startInfo)
?? throw new InvalidOperationException("Could not start lease fixture.");
try
{
await WaitForFileAsync(ready, helper, TimeSpan.FromSeconds(10));
Assert.True(File.Exists(staging));
var blockedInstaller = new LauncherInstaller(
_paths,
_bakeExecutable,
recordStore: new LauncherInstallRecordStore(_paths));
using var blockedCancellation = new CancellationTokenSource(
TimeSpan.FromMilliseconds(200));
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => blockedInstaller.LoadExistingAsync(blockedCancellation.Token));
Assert.True(File.Exists(staging));
}
finally
{
if (!helper.HasExited)
{
helper.Kill(entireProcessTree: true);
}
await helper.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(10));
}
var restarted = new LauncherInstaller(
_paths,
_bakeExecutable,
recordStore: new LauncherInstallRecordStore(_paths));
InstallRecordVerification recovered = await restarted.LoadExistingAsync();
Assert.True(recovered.IsVerified);
Assert.Equal(old, recovered.Record);
Assert.False(File.Exists(staging));
Assert.Equal(
"previous verified package",
await File.ReadAllTextAsync(store.PreparedAssetPath));
}
private async Task<(
LauncherInstaller Installer,
LauncherInstallRecordStore Store,
@ -303,6 +512,80 @@ public sealed class LauncherInstallerTests : IDisposable
return (installer, store, old);
}
private async Task<LauncherInstallRecord> CreatePriorRecordAsync(
LauncherInstallRecordStore store)
{
Directory.CreateDirectory(Path.GetDirectoryName(store.PreparedAssetPath)!);
await File.WriteAllTextAsync(
store.PreparedAssetPath,
"previous verified package");
var old = new LauncherInstallRecord(
Path.GetFullPath(_dats),
Path.GetFullPath(store.PreparedAssetPath),
await FileIntegrity.ComputeSha256HexAsync(store.PreparedAssetPath),
new FileInfo(store.PreparedAssetPath).Length,
LauncherInstallRecordStore.CurrentBakeToolVersion);
await store.SaveAtomicallyAsync(old);
return old;
}
private static async Task WaitForFileAsync(
string path,
Process process,
TimeSpan timeout)
{
using var cancellation = new CancellationTokenSource(timeout);
while (!File.Exists(path))
{
if (process.HasExited)
{
throw new InvalidOperationException(
$"Lease fixture exited with {process.ExitCode}: "
+ await process.StandardError.ReadToEndAsync());
}
await Task.Delay(25, cancellation.Token);
}
}
private static string GetInstallLeaseFixturePath()
{
string root = FindRepositoryRoot();
string configuration = new DirectoryInfo(AppContext.BaseDirectory)
.Parent?.Name
?? "Release";
return Path.Combine(
root,
"tests",
"AcDream.Launcher.Core.Tests.Fixtures.InstallLeaseHolder",
"bin",
configuration,
"net10.0",
"AcDream.Launcher.Core.Tests.Fixtures.InstallLeaseHolder.dll");
}
private static string FindRepositoryRoot()
{
foreach (string start in new[]
{
AppContext.BaseDirectory,
Environment.CurrentDirectory,
})
{
for (var directory = new DirectoryInfo(start);
directory is not null;
directory = directory.Parent)
{
if (File.Exists(Path.Combine(directory.FullName, "AcDream.slnx")))
{
return directory.FullName;
}
}
}
throw new DirectoryNotFoundException("Could not locate repository root.");
}
private static void CreateCompleteDatDirectory(string directory)
{
Directory.CreateDirectory(directory);