Repository navigation
handlers: do not append an error envelope to a response already streaming - #4938
Open
nishthaGupta wants to merge 1 commit into
Open
nishthaGupta wants to merge 1 commit into
nishthaGupta wants to merge 1 commit into
Conversation
…ming When a handler fails part way through writing the response body, the dispatcher still called errcode.ServeJSON. The status line and headers are already on the wire at that point, so net/http discards the second WriteHeader and logs "superfluous response.WriteHeader call", but the JSON is still encoded onto the connection. The client receives a truncated payload with an error object appended to it, under the original status and a Content-Length it never reaches. It also makes the request log inaccurate: errcode.ServeJSON sets Content-Type: application/json and calls WriteHeader(500), and while net/http ignores both, instrumentedResponseWriter records them, so GetResponseLogger reports status 500 and content type application/json for a response the client received as 200 application/octet-stream. Skip the envelope once body bytes have been written and log the errors instead. Header-only writes are unaffected, so a handler that sets its own status but writes no body still gets an envelope, which is the existing behaviour for layer upload range errors. This does not change which requests succeed or fail. A response that reaches this path was already truncated. It stops the registry writing JSON onto the tail of that truncated body, and keeps the logged status and content type describing what was actually sent. Signed-off-by: Nishtha Gupta <[email protected]>
Member
|
Hey @nishthaGupta can you rebase? |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4937
Problem
When a handler fails part way through writing the response body, the dispatcher still calls
errcode.ServeJSONin its deferred error handling (registry/handlers/app.go). The status line and headers are already sent, sonet/httpdiscards the secondWriteHeaderand logshttp: superfluous response.WriteHeader call, but the JSON is still encoded onto the connection. The client ends up with a truncated payload plus an error object appended to it, under the original status and aContent-Lengththat is never reached.It also makes the request log inaccurate.
errcode.ServeJSONsetsContent-Type: application/jsonand callsWriteHeader(500), and whilenet/httpignores both,instrumentedResponseWriterrecords them, soGetResponseLoggerreportshttp.response.status=500 http.response.contenttype=application/jsonfor a response the client received as200 application/octet-stream.This shows up in proxy (pull-through cache) mode when an upstream read fails mid-layer. On one deployment over a week, 67 requests had written more than 100 KB of layer data before the envelope went out, the largest 71 MB.
Fix
Skip the envelope once body bytes have been written, and log the errors instead.
written > 0comes fromhttp.response.written, whichinstrumentedResponseWriteralready exposes viaContext.Value, so no new plumbing is needed.Header-only writes are deliberately unaffected: a handler that sets its own status but writes no body can still have an envelope appended, which is the existing behaviour that layer upload range errors depend on.
This does not change which requests succeed or fail. A response that reached this path was already truncated. It stops the registry writing JSON onto the tail of that truncated body, and it keeps the logged status and content type describing what was actually sent.
Diff
Tests
TestDispatcherErrorEnvelopeAfterResponseStartedinregistry/handlers/app_test.gocovers both branches: a handler that writes partial content keeps its body and status, and a handler that writes nothing still gets the envelope. It reuses the existinginmemorydriver plusmemorycachescaffolding already used byTestAppDispatcher, and addsioto the test imports.Notes for reviewers
http.response.writtenthroughContext.Valuekeeps the change to one file and matches how the surrounding code readshttp.response.status. Happy to switch to asserting on theResponseWriterif you would rather not depend on the context key here.Verifying the test catches it
With only the test applied and
app.goleft as-is onmain:With the fix, both subtests pass and the full
./registry/handlers/package is green.