Skip to content

net/http: no way to get server Handler's write error to client (sometimes) #28271

Open
@muirdm

Description

@muirdm

What version of Go are you using (go version)?

1.11

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

linux/amd64

What did you do?

I wrote an http response after the client socket was closed.

https://play.golang.org/p/upytSYnrYmL

What did you expect to see?

I expected to be able to get the write error somehow.

What did you see instead?

There does not seem to be any way to get the write error, even after flushing.

More background:

The use case is we had an http handler that took too long to respond. After figuring out what happened, we wanted to make sure our http server logged all errors writing responses so it would be easy to see this happening in the future.

In our particular case, we weren't actually writing a body (we only called WriteHeader). I was hoping I could do something like this in a middleware to log response errors in a generic way:

nextHandler(w, r)
if flusher, ok := w.(http.Flusher); ok {
  flusher.Flush()
  if _, err := w.Write(nil); err != nil {
    // log "err" for posterity
  }
}

Perhaps this could be achieved by having http.response.write return conn.werr if it is set?

It is worth mentioning that the request context is cancelled in this case, so that could be used to detect the client is gone. However, I expected there to be someway to get the actual Write error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions