-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"runtime" | ||
"time" | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 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() | ||
|
There was a problem hiding this comment.
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?
moby/vendor/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/wrap.go
Lines 93 to 99 in f9dfd13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our Flusher calls
Flush()
on the stdlib implementation, not on the OTEL wrapper because it doesn't wrap theFlush()
func. WriteHeader(), so if we call this here we are settingw.wroteHeader
to true on the OTEL wrapper, so that subsequent calls to the OTEL wrapper'sWrite()
func will NOT call the OTEL wrapper'sWriteHeader()
func.example of current sitation:
calls to
flusher.Flush()
;Flush(
) gets called byflusher.Flush()
;Write()
gets called by stdlibFlush()
;WriteHeader()
gets called by stdlibWrite()
calls to
flusher.Write()
:Write()
gets called byflusher.Write()
;WriteHeader()
gets called by OTEL wrapperWrite()
- only when it hasn't been called before;WriteHeader()
gets called by OTEL wrapperWriteHeader()
;Write()
gets called by OTEL wrapperWrite()
;flusher.Flush()
gets called byflusher.Write()
;Sorry for the verbosity, too much wrapping confusion here :)
There was a problem hiding this comment.
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.