Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix superfluous response.WriteHeader call #47715

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion api/server/httputils/write_log_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"sort"

Expand All @@ -17,9 +18,14 @@ import (
// WriteLogStream writes an encoded byte stream of log messages from the
// messages channel, multiplexing them with a stdcopy.Writer if mux is true
func WriteLogStream(_ context.Context, w io.Writer, msgs <-chan *backend.LogMessage, config *container.LogsOptions, mux bool) {
// Used before the Flush(), so we don't set the header twice because of the otel wrapper
// see here: https://github.com/moby/moby/issues/47448
if rw, ok := w.(http.ResponseWriter); ok {
rw.WriteHeader(http.StatusOK)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to figure out how writing this here would prevent OTEL from trying to write a header again; wouldn't it still hit the "already wrote header" case?

func (w *respWriterWrapper) WriteHeader(statusCode int) {
if !w.wroteHeader {
w.wroteHeader = true
w.statusCode = statusCode
}
w.ResponseWriter.WriteHeader(statusCode)
}

Copy link
Contributor Author

@krissetto krissetto Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to figure out how writing this here would prevent OTEL from trying to write a header again; wouldn't it still hit the "already wrote header" case?

func (w *respWriterWrapper) WriteHeader(statusCode int) {
if !w.wroteHeader {
w.wroteHeader = true
w.statusCode = statusCode
}
w.ResponseWriter.WriteHeader(statusCode)
}

Our Flusher calls Flush() on the stdlib implementation, not on the OTEL wrapper because it doesn't wrap the Flush() func. WriteHeader(), so if we call this here we are setting w.wroteHeader to true on the OTEL wrapper, so that subsequent calls to the OTEL wrapper's Write() func will NOT call the OTEL wrapper's WriteHeader() func.


example of current sitation:

calls to flusher.Flush();

  • stdlib Flush() gets called by flusher.Flush();
  • stdlib Write() gets called by stdlib Flush();
  • stdlib WriteHeader() gets called by stdlibWrite()

calls to flusher.Write():

  • OTEL wrapper Write() gets called by flusher.Write();
  • OTEL wrapper WriteHeader() gets called by OTEL wrapper Write() - only when it hasn't been called before;
  • stdlib WriteHeader() gets called by OTEL wrapper WriteHeader();
  • stdlib Write() gets called by OTEL wrapper Write();
  • flusher.Flush() gets called by flusher.Write();
  • ... repeat above flow

Sorry for the verbosity, too much wrapping confusion here :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we either to need to manually instrument the streaming responses or provide a smarter writer implementation to the daemon so that the first flush calls WriteHeader explicitly.

}

wf := ioutils.NewWriteFlusher(w)
defer wf.Close()

wf.Flush()
krissetto marked this conversation as resolved.
Show resolved Hide resolved

outStream := io.Writer(wf)
Expand Down
7 changes: 7 additions & 0 deletions daemon/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"net/http"
"runtime"
"time"

Expand Down Expand Up @@ -46,6 +47,12 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c

outStream := config.OutStream
if config.Stream {
// Used before the Flush(), so we don't set the header twice because of the otel wrapper
// see here: https://github.com/moby/moby/issues/47448
if rw, ok := outStream.(http.ResponseWriter); ok {
rw.WriteHeader(http.StatusOK)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the right place to handle this.

I'm also not sure how this fix the otel issue since the very first write to the stream would be writing out the response header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the right place to handle this.
...
It seems like we either to need to manually instrument the streaming responses or provide a smarter writer implementation to the daemon so that the first flush calls WriteHeader explicitly.

Yeah i mostly agree with this, but there's another couple things to consider here:

I have opened another draft pr yesterday, moving the current WriteFlusher impl and making a new internal one in /internal/writeflusher with these changes.

If we merge that, this PR can be closed as it'd be unnecessary, but I left it here in case we decide on a different strategy and we only want a quick fix for now.

}

wf := ioutils.NewWriteFlusher(outStream)
defer wf.Close()
wf.Flush()
Expand Down