Repository navigation
registry: fix access log showing response size 0 when using ReadFrom - #5103
Conversation
| h.ServeHTTP(w, r) | ||
| _, _ = io.WriteString(out, combinedLogLine(r, u, ts, status, written)) |
There was a problem hiding this comment.
The old handler has a small behaviour that’s easy to miss. After the downstream handler returns, Gorilla checks req.MultipartForm and removes any temporary files. We write the log line here.
net/http normally handles cleanup on the original request, and our otel wrapper explicitly copies the form back for that reason. The case I’m not entirely sure about is registered middleware that makes its own request copy. If the form is attached to the copy the logger sees, but never makes it back to the server’s request, Gorilla used to clean it up. Who owns those files now?
I tried rereplicating a leak in the registry using my agent but havnt seen any. I’d keep the existing cleanup in this PR unless we can account for that middleware case. It would be easy to lose this behavior in a change that looks like it only touches logging.
There was a problem hiding this comment.
Pushed a commit to restore cleanup. The access logger now calls RemoveAll() on the form attached to the copy it received.
Here is a summary:
Cleanup happens after the upload handler returns to the access logger, and before the access logger returns to the middleware.
- The middleware makes a request copy and hands it to the access logger.
- The access logger calls the upload handler with that copy. The upload handler parses the form, creates a temporary file, and returns to the logger.
- This is where I added cleanup. The logger still holds the copy, so it calls RemoveAll() on the copy’s form. That deletes the file.
- The logger writes the access log entry and returns to the middleware.
(The server’s original request never knew about the file. Cleanup works because the logger holds the copy that does know about it.)
Tested your exact scenario through RegisterHandler: the original request had a nil form in both runs.
Existing issue in main found by Astra: #5106
There was a problem hiding this comment.
Adding a separate commit for addressing #5106.
369da6b to
a1481c3
Compare
milosgajdos
left a comment
There was a problem hiding this comment.
LGTM, but can you squash the commits, @jgehrcke, please
ca16e55 to
61b5957
Compare
Messages of squashed commits: registry: fix access log size 0 for ReadFrom bodies (coming with distribution#5052) Gorilla's combined logging handler counts response bytes in `Write` only. For a body copied through `ReadFrom` it logs size 0 (gorilla/handlers#284). This only happens from distribution#5052 on, which copies filesystem blob GET bodies through `ReadFrom`. Replace Gorilla's handler with a local one that writes the same log line and counts the bytes of `Write` and `ReadFrom` with httpsnoop hooks. The tests compare its log line with Gorilla's and cover copies through `ReadFrom`. -------------------- registry: fix access log status after a flush When a handler flushes and then calls `WriteHeader(503)`, the client gets 200 but the access log shows 503. Flushing commits the response status before the handler sets 503. Track the committed header in the `Flush` and `FlushError` hooks. The client-status test flushes with both `http.Flusher` and `http.ResponseController`. Both cases fail without the hooks. -------------------- registry: restore multipart file cleanup in the access logger Middleware can pass a copy of the request to the access logger. If the handler parses a multipart upload on that copy, the server's original request has no form to clean up. Any upload spilled to disk remains after the request. Restore Gorilla's call to `MultipartForm.RemoveAll` after the handler returns. As in Gorilla, skip the log line if cleanup fails. The test parses a file on a request copy and checks that the file is gone when the logger returns. Without the fix the file still opens. -------------------- registry: avoid quoting plain ASCII access log fields `quoteLogField` splits and quotes even an empty Referer or a user agent like `docker/29.0.0`. Both already contain exactly what the log needs. Return the input string for printable ASCII without quotes or backslashes. Keep the existing escaping for all other fields, including Gorilla's special spelling for DEL. -------------------- registry: clean up multipart files on dispatched request copies A blob upload request can leave a temporary file behind even when authentication fails. Authorization reads the source repository with `FormValue`, which can parse a multipart body on a request copy. The server cannot clean up a form it never sees. Remove the form on return from the dispatcher if it was parsed there or on the copy passed to the upload handler. A form already present on entry remains the caller's responsibility. The regression test checks the temporary directory after both a 401 and a 202. Both cases leave a file behind without the fix. Fixes distribution#5106. -------------------- Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
61b5957 to
841b85e
Compare
|
Squashed. Thanks for your time and the collaboration, Milos! |
Extracted from #5052.
This is a pragmatic fix for gorilla/handlers#284.
Gorilla won't make releases anymore. We agree that cncf distribution must move away from Gorilla at some point. But before doing that "all the way" (ripping it out properly), I set out to find a way to address gorilla/handlers#284 somewhat more pragmatically. The result is this patch here.
It replaces Gorilla's access log handler with a custom one that writes the same log format, but correctly counts the response bytes written via both,
WriteandReadFrom(with httpsnoop hooks)Much of this, including the tests, was written with the help of AI. The set of tests tries to make rather damn sure that we really do not change the user-facing logging behavior as compared to the original Gorilla log handler. Still, I'd appreciate a more or less careful review.
This also fixes #5111.