From e6d987882eec98e0ced1b11858921401abecb705 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Tue, 29 Oct 2024 16:29:02 +0100 Subject: [PATCH] API: container logs flush status code API clients expect the status code quickly otherwise they can time out. If we do not flush we may not write the header immediately and only when futher logs are send. Fixes #23712 Signed-off-by: Paul Holzinger --- pkg/api/handlers/compat/containers_logs.go | 11 ++++++++--- test/apiv2/20-containers.at | 7 +++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/api/handlers/compat/containers_logs.go b/pkg/api/handlers/compat/containers_logs.go index b1440ff9dd..f956a2b05c 100644 --- a/pkg/api/handlers/compat/containers_logs.go +++ b/pkg/api/handlers/compat/containers_logs.go @@ -106,6 +106,13 @@ func LogsFromContainer(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) + flush := func() { + if flusher, ok := w.(http.Flusher); ok { + flusher.Flush() + } + } + flush() + var frame strings.Builder header := make([]byte, 8) @@ -167,8 +174,6 @@ func LogsFromContainer(w http.ResponseWriter, r *http.Request) { if _, err := io.WriteString(w, frame.String()); err != nil { log.Errorf("unable to write frame string: %q", err) } - if flusher, ok := w.(http.Flusher); ok { - flusher.Flush() - } + flush() } } diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 41039f22d7..c2737f006a 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -150,6 +150,13 @@ podman run --name $CTRNAME -d $IMAGE sleep 25 t GET containers/$CTRNAME/top?stream=false 200 \ .Processes.[0].[6]="00:00:00" \ .Processes.[0].[7]="sleep 25" + +# check logs output, IMPORTANT the container should write no logs to reproduce #23712 +APIV2_TEST_EXPECT_TIMEOUT=1 t GET "containers/${CTRNAME}/logs?follow=true&stdout=true&stderr=true" 999 +is "" "$(<$WORKDIR/curl.result.out)" "Container MUST NOT log output" +like "$(<$WORKDIR/curl.headers.out)" ".*HTTP.* 200 OK.*" \ + "Received headers from /container//logs" + podman rm -f -t0 $CTRNAME CTRNAME=test123