fix(server): the current log downloads while the server keeps writing it
GET /api/v1/logs/files/{name} served the file with Results.File, which sets
Content-Length from the file's size and then copies it to its end. The current
log grows whenever the server logs, so a line written meanwhile made Kestrel
abort the response ("too many bytes written"); AdminTests failed on it once in
three full runs. The endpoint now sends the bytes the file held when it was
listed through FileResponses.Serve, a fixed-length, range-capable slice.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
2 files changed
+36
-3
No files matched your search
@@ -29,10 +29,11 @@ public static class AdminEndpoints
|
||||
ProtocolJson.Default.ListLogFileDto));
|
||||
logs.MapGet("/files/{name}", (HttpContext context, string name, ServerOptions options) =>
|
||||
{
|
||||
// Only names of existing log files are served: the name never becomes part of a path.
|
||||
// Only names of existing log files are served: the name never becomes part of a path. The current log keeps
|
||||
// growing while it is sent, so the response holds the bytes it had when listed, as its Content-Length says.
|
||||
if (LogFiles(options).FirstOrDefault(f => f.Name == name) is not { } file) return ApiErrors.NotFound(context, "log");
|
||||
context.Response.Headers.CacheControl = "no-store";
|
||||
return Results.File(file.FullName, "text/plain; charset=utf-8", file.Name, file.LastWriteTimeUtc, enableRangeProcessing: true);
|
||||
return FileResponses.Serve(context, file, 0, file.Length, "text/plain; charset=utf-8", "no-store", downloadName: file.Name)
|
||||
?? ApiErrors.NotFound(context, "log");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Titano.Protocol;
|
||||
using Titano.Testing;
|
||||
|
||||
@@ -66,6 +68,36 @@ public sealed class AdminTests(TestContext context)
|
||||
await traversal.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheCurrentLogDownloadsWhileTheServerKeepsLogging()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.LogLevel = Microsoft.Extensions.Logging.LogLevel.Information);
|
||||
var admin = await server.AdminAsync();
|
||||
using var files = await admin.GetAsync("/api/v1/logs/files");
|
||||
var name = (await files.ReadAsync(ProtocolJson.Default.ListLogFileDto)).First().Name;
|
||||
var logger = server.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Titano.Server.Tests");
|
||||
using var stop = new CancellationTokenSource();
|
||||
// Jobs, uploads and sign-ins keep writing to the file an administrator downloads.
|
||||
var logging = Task.Run(() =>
|
||||
{
|
||||
while (!stop.IsCancellationRequested) logger.LogInformation("Busy");
|
||||
});
|
||||
try
|
||||
{
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
using var download = await admin.GetAsync($"/api/v1/logs/files/{name}");
|
||||
await download.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Equal(download.Content.Headers.ContentLength, (await download.Content.ReadAsByteArrayAsync()).LongLength);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
await stop.CancelAsync();
|
||||
await logging;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ArchiveSettingsValidateTemplatesAndJobsNeedHandlers()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user