fix(rest): preserve ResponseController through detailed logging by leehainuo · Pull Request #5828 · zeromicro/go-zero · GitHub
Skip to content

fix(rest): preserve ResponseController through detailed logging - #5828

Open
leehainuo wants to merge 1 commit into
zeromicro:masterfrom
leehainuo:fix/detailed-log-response-controller
Open

leehainuo wants to merge 1 commit into
zeromicro:masterfrom
leehainuo:fix/detailed-log-response-controller

Conversation

@leehainuo

@leehainuo leehainuo commented Oct 13, 2026 •

Copy link
Copy Markdown

What problem does this PR solve?

DetailedLogHandler wraps WithCodeResponseWriter with
detailLoggedResponseWriter, but the outer writer does not implement
Unwrap().

As a result, http.ResponseController cannot reach capabilities provided by
the underlying http.ResponseWriter.

This affects SSE routes in particular. SSE routes use
ResponseController.SetWriteDeadline(time.Time{}) to clear the server write
deadline, but the call fails with feature not supported when the response
writer is wrapped by DetailedLogHandler.

How is it fixed?

Add Unwrap() to detailLoggedResponseWriter and return its immediate
underlying WithCodeResponseWriter.

This preserves the existing response-writer unwrapping chain:

detailLoggedResponseWriter
    -> WithCodeResponseWriter
    -> underlying http.ResponseWriter

WithCodeResponseWriter already implements the same Unwrap() contract for
http.ResponseController.

Reproduction

I reproduced the issue with a real SSE route using:

  • Verbose: true
  • Timeout: 500ms
  • one SSE event per second
  • Accept: text/event-stream

Before the fix, go-zero fails to clear the SSE write deadline:

unable to clear write deadline for SSE connection: feature not supported

The client only receives the first event before the connection is closed:

data: 1

curl: (18) transfer closed with outstanding read data remaining

Later server writes also fail with an I/O timeout:

write 4: write tcp ...: i/o timeout
write 5: write tcp ...: i/o timeout

After adding Unwrap() to detailLoggedResponseWriter, the same reproduction
receives all five events successfully.

Tests

Added a regression test with an underlying response writer that supports
SetWriteDeadline.

Before the fix, ResponseController.SetWriteDeadline fails with
feature not supported.

After the fix, the call succeeds through DetailedLogHandler.

Validated with:

go test ./rest/handler -count=1
go test -race ./rest/handler -count=1
go test ./rest/... -count=1
go test -race ./rest/... -count=1
go test ./... -count=1
go test -race ./... -count=1

@leehainuo

Copy link
Copy Markdown
Author

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant