Skip to content

Commit

Permalink
API: container logs flush status code
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Luap99 committed Nov 1, 2024
1 parent 768ad86 commit e6d9878
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/api/handlers/compat/containers_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
}
}
7 changes: 7 additions & 0 deletions test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -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/<id>/logs"

podman rm -f -t0 $CTRNAME

CTRNAME=test123
Expand Down

0 comments on commit e6d9878

Please sign in to comment.