Skip to content

registry: fix access log showing response size 0 when using ReadFrom - #5103

Merged
milosgajdos merged 1 commit into
distribution:mainfrom
jgehrcke:jp/accesslog-readfrom
Nov 27, 2026
Merged

milosgajdos merged 1 commit into
distribution:mainfrom
jgehrcke:jp/accesslog-readfrom

Conversation

@jgehrcke

@jgehrcke jgehrcke commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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, Write and ReadFrom (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.

@github-actions github-actions Bot added the dependencies Pull requests that update a dependency file label Nov 26, 2026
@jgehrcke jgehrcke changed the title registry: fix access log size 0 for ReadFrom bodies (coming with #5052) registry: fix access log showing response size 0 when using ReadFrom Nov 26, 2026
Comment thread registry/accesslog.go Resolved
Comment thread registry/accesslog.go
Comment on lines +58 to +59
h.ServeHTTP(w, r)
_, _ = io.WriteString(out, combinedLogLine(r, u, ts, status, written))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

  1. The middleware makes a request copy and hands it to the access logger.
  2. 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.
  3. 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.
  4. 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Adding a separate commit for addressing #5106.

Comment thread registry/accesslog.go Resolved
Comment thread registry/accesslog.go Resolved

@milosgajdos milosgajdos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, but can you squash the commits, @jgehrcke, please

@jgehrcke
jgehrcke force-pushed the jp/accesslog-readfrom branch from ca16e55 to 61b5957 Compare November 27, 2026 07:47
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]>
@jgehrcke
jgehrcke force-pushed the jp/accesslog-readfrom branch from 61b5957 to 841b85e Compare November 27, 2026 07:50
@jgehrcke

jgehrcke commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Squashed. Thanks for your time and the collaboration, Milos!

@milosgajdos
milosgajdos merged commit efbe3cf into distribution:main Nov 27, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api dependencies Pull requests that update a dependency file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

registry: access log shows status 500 when the client received 200

2 participants