转到http.MaxBytesReader重置连接

I am trying to implement a restriction on file size when uploading files. The following code correctly detects whenever the file (or rather the request body, but close enough for my purposes) is larger than 1 MB. If this is the case however, the page returned is a "The connection was reset" page, instead of a page with my custom error message. If the file is under 1 MB I correctly get a page saying "File upload OK".

I would love some pointers on why the connection to the server is reset instead of serving what I specify in the code below.

func baseHandler(writer http.ResponseWriter, request *http.Request) {
    request.Body = http.MaxBytesReader(writer, request.Body, 1024 * 1024)
    _, _, err := request.FormFile("uploadfile")

    if err != nil {
        fmt.Fprintf(writer, "ERROR: %v", err) // Should be displayed in browser, is not
        fmt.Printf("ERROR: %v", err) // Gets written to terminal, so any errors are correctly identified

        return
    }

    fmt.Fprintf(writer, "File upload OK")
}


func main() {
    http.HandleFunc("/", baseHandler)
    http.ListenAndServe(":8080", nil)
}

The server does write the response. The issue is that the client does not read the response.

When the MaxBytesReader limit is breached, the server stops reading data from the client. Also, the server fully closes the connection a half second after writing the response to the client.

Many HTTP clients write the complete request body before reading the response and stop on any error writing the request body. These clients report "connection reset" errors and the like when the request body is sufficiently large and ignore the response written by the server.

Server code pointers: When the MaxBytesReader limit is breached, the response's requestBodyLimitHit field is set to true. All relevant code is near uses of this field.