Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: fix Downloader to dispose tempFile and use synchronous IO#81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
spikecurtis merged 2 commits intomainfromspike/internal-598-temp-dir
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletionsTests.Vpn.Service/DownloaderTest.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -442,23 +442,28 @@ public async Task CancelledWaitingForOther(CancellationToken ct)
[CancelAfter(30_000)]
public async Task CancelledInner(CancellationToken ct)
{
var httpCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
var taskCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
using var httpServer = new TestHttpServer(async ctx =>
{
ctx.Response.StatusCode = 200;
await ctx.Response.OutputStream.WriteAsync("test"u8.ToArray(), ct);
await ctx.Response.OutputStream.FlushAsync(ct);
await Task.Delay(TimeSpan.FromSeconds(5), ct);
await ctx.Response.OutputStream.WriteAsync("test"u8.ToArray(), httpCts.Token);
await ctx.Response.OutputStream.FlushAsync(httpCts.Token);
// wait up to 5 seconds.
await Task.Delay(TimeSpan.FromSeconds(5), httpCts.Token);
});
var url = new Uri(httpServer.BaseUrl + "/test");
var destPath = Path.Combine(_tempDir, "test");

var manager = new Downloader(NullLogger<Downloader>.Instance);
// The "inner" Task should fail.
varsmallerCt =new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token;
vartaskCt =taskCts.Token;
var dlTask = await manager.StartDownloadAsync(new HttpRequestMessage(HttpMethod.Get, url), destPath,
NullDownloadValidator.Instance, smallerCt);
NullDownloadValidator.Instance, taskCt);
await taskCts.CancelAsync();
var ex = Assert.ThrowsAsync<TaskCanceledException>(async () => await dlTask.Task);
Assert.That(ex.CancellationToken, Is.EqualTo(smallerCt));
Assert.That(ex.CancellationToken, Is.EqualTo(taskCt));
await httpCts.CancelAsync();
}

[Test(Description = "Validation failure")]
Expand Down
26 changes: 12 additions & 14 deletionsVpn.Service/Downloader.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -453,27 +453,25 @@ private async Task Start(CancellationToken ct = default)
if (res.Content.Headers.ContentLength >= 0)
TotalBytes = (ulong)res.Content.Headers.ContentLength;

FileStream tempFile;
try
{
tempFile = File.Create(TempDestinationPath, BufferSize,
FileOptions.Asynchronous | FileOptions.SequentialScan);
}
catch (Exception e)
{
_logger.LogError(e, "Failed to create temporary file '{TempDestinationPath}'", TempDestinationPath);
throw;
}

await Download(res, tempFile, ct);
await Download(res, ct);
return;
}

private async Task Download(HttpResponseMessage res,FileStream tempFile,CancellationToken ct)
private async Task Download(HttpResponseMessage res, CancellationToken ct)
{
try
{
var sha1 = res.Headers.Contains("ETag") ? SHA1.Create() : null;
FileStream tempFile;
try
{
tempFile = File.Create(TempDestinationPath, BufferSize, FileOptions.SequentialScan);
}
catch (Exception e)
{
_logger.LogError(e, "Failed to create temporary file '{TempDestinationPath}'", TempDestinationPath);
throw;
}
await using (tempFile)
{
var stream = await res.Content.ReadAsStreamAsync(ct);
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp