Skip to content

caddyhttp: surface write timeout errors in access log - #7945

Merged
francislavoie merged 1 commit into
caddyserver:masterfrom
faiyazrahmann:fix-7938-access-log-write-timeout
Sep 4, 2026
Merged

francislavoie merged 1 commit into
caddyserver:masterfrom
faiyazrahmann:fix-7938-access-log-write-timeout

Conversation

@faiyazrahmann

Copy link
Copy Markdown
Contributor

Fixes #7938

Adds a write_error field to the access log with the real flush error on write timeouts, instead of silently logging status 200 for a response the client never got. status/size/resp_headers are left as the attempted response, per discussion in the issue.

  • Gated to HTTP/1.x, duration >= WriteTimeout
  • Uses the existing FlushError() on the recorder, swallows ErrHijacked
  • Test added for the new field, existing logRequest tests/benchmarks updated for the new param

Reproduced locally (reverse_proxy to a slow backend + write 2s), curl still gets an empty reply, but the log now shows the actual i/o timeout error.

AI disclosure: AI was used during the initial investigation to help reproduce and trace the issue. The implementation, tests, and final changes were written and verified by myself.
write-error

Copilot AI lite review requested due to automatic review settings August 17, 2026 08:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Caddy’s HTTP server access logging to surface client write failures (notably HTTP/1.x write timeout i/o errors) by adding a new write_error field, so logs no longer imply a successful response when the client received none.

Changes:

  • Track and flush HTTP/1.x responses after handler completion (when duration ≥ WriteTimeout) to observe terminal write errors before access logging.
  • Extend access log emission to include a write_error field when a terminal flush/write error is detected (excluding http.ErrHijacked).
  • Update existing logRequest tests/benchmarks for the new parameter and add a unit test for the write_error field output.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
modules/caddyhttp/server.go Adds write-timeout flush observation and includes a new write_error access log field.
modules/caddyhttp/server_test.go Updates existing logRequest tests/benchmarks for the new signature and adds coverage for emitting write_error.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread modules/caddyhttp/server.go Resolved
@faiyazrahmann
faiyazrahmann force-pushed the fix-7938-access-log-write-timeout branch 2 times, most recently from 7fc15db to 1c0ac03 Compare August 17, 2026 09:10
Comment thread modules/caddyhttp/server.go Resolved
@faiyazrahmann
faiyazrahmann force-pushed the fix-7938-access-log-write-timeout branch from 1c0ac03 to d50474c Compare August 17, 2026 12:54
Comment thread modules/caddyhttp/server.go Resolved
@faiyazrahmann
faiyazrahmann force-pushed the fix-7938-access-log-write-timeout branch from b8fad79 to 6b741c0 Compare September 3, 2026 10:32

@steadytao steadytao 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.

I am happy with this now. LGTM.

@steadytao

Copy link
Copy Markdown
Member

I will let you review before merge @WeidiDeng - please take your time.

Comment thread modules/caddyhttp/server.go Resolved
Access logs recorded status 200 and full resp_headers on write
timeouts, even though the client never received a response. Adds
a write_error field with the actual flush error, gated to HTTP/1.x
requests past the configured write timeout. status, size, and
resp_headers still reflect the attempted response.

Fixes caddyserver#7938

Co-authored-by: Eugin Francis <13235453+euginfrancis@users.noreply.github.com>
Signed-off-by: Faiyaz Rahman <faiyazrahman03@gmail.com>
@faiyazrahmann
faiyazrahmann force-pushed the fix-7938-access-log-write-timeout branch from 6b741c0 to 882f6bc Compare September 4, 2026 07:43
@faiyazrahmann

Copy link
Copy Markdown
Contributor Author

@WeidiDeng fixed. kindly check if it works.

  • used http.NewResponseController(wrec).Flush().
  • Added an http.ErrNotSupported check alongside the existing ErrHijacked one, since the controller returns that instead of a silent no-op.

@francislavoie francislavoie added this to the v2.11.5 milestone Sep 4, 2026
@francislavoie francislavoie added the feature ⚙️ New feature or request label Sep 4, 2026
@francislavoie
francislavoie merged commit 8e90c9a into caddyserver:master Sep 4, 2026
29 checks passed
@faiyazrahmann
faiyazrahmann deleted the fix-7938-access-log-write-timeout branch September 5, 2026 05:58
jotikrishna pushed a commit to jotikrishna/caddy that referenced this pull request Oct 7, 2026
Since caddyserver#7945, Server.ServeHTTP flushes the response in a deferred func when an
HTTP/1.x request that is being access-logged took at least write_timeout. The
deferred func already skips http.ErrHijacked, on the assumption that Flush
reports a hijacked response that way.

It does not. (*http.response).Hijack releases the bufio.Writer, and
(*http.response).FlushError calls w.w.Flush() without checking for the hijack.
Only Write and WriteHeader consult hijacked() and return ErrHijacked.

So when the handler hijacked the connection, as reverse_proxy does when it
passes through a 101 Switching Protocols upgrade, and the upgraded stream
outlives write_timeout, the deferred flush reaches a nil bufio.Writer and
panics with an invalid memory address. net/http recovers it and it fires only
after the upgraded stream ended, so clients never notice, but every such
connection ends with an error-level stack trace in the logs.

responseRecorder already tracks the hijack for DetachAfterHijack and
WriteResponse, so report it from FlushError too, which is what the deferred
func expects.

Fixes caddyserver#8151
jotikrishna added a commit to jotikrishna/caddy that referenced this pull request Oct 7, 2026
Since caddyserver#7945, Server.ServeHTTP flushes the response in a deferred func when an
HTTP/1.x request that is being access-logged took at least write_timeout. The
deferred func already skips http.ErrHijacked, on the assumption that Flush
reports a hijacked response that way.

It does not. (*http.response).Hijack releases the bufio.Writer, and
(*http.response).FlushError calls w.w.Flush() without checking for the hijack.
Only Write and WriteHeader consult hijacked() and return ErrHijacked.

So when the handler hijacked the connection, as reverse_proxy does when it
passes through a 101 Switching Protocols upgrade, and the upgraded stream
outlives write_timeout, the deferred flush reaches a nil bufio.Writer and
panics with an invalid memory address. net/http recovers it and it fires only
after the upgraded stream ended, so clients never notice, but every such
connection ends with an error-level stack trace in the logs.

responseRecorder already tracks the hijack for DetachAfterHijack and
WriteResponse, so report it from FlushError too, which is what the deferred
func expects.

Fixes caddyserver#8151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature ⚙️ New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Access log writes status code 200 on timeout even though no response is returned

5 participants