From 9f03c07fdfe2cd378156f627edaa1f36ab8d1715 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 6 Oct 2026 21:19:30 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Server/Api/AdminEndpoints.cs | 7 +++-- tests/Titano.Server.Tests/Api/AdminTests.cs | 32 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Titano.Server/Api/AdminEndpoints.cs b/src/Titano.Server/Api/AdminEndpoints.cs index 45826700..a575e7d5 100644 --- a/src/Titano.Server/Api/AdminEndpoints.cs +++ b/src/Titano.Server/Api/AdminEndpoints.cs @@ -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"); }); } diff --git a/tests/Titano.Server.Tests/Api/AdminTests.cs b/tests/Titano.Server.Tests/Api/AdminTests.cs index e085f171..2f8b00e8 100644 --- a/tests/Titano.Server.Tests/Api/AdminTests.cs +++ b/tests/Titano.Server.Tests/Api/AdminTests.cs @@ -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().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() {