Skip to content

net/http: provide way to get Request write error and HTTP response? #11745

Closed as not planned
@tarm

Description

@tarm

(As Brad mentioned on go-nuts, this is likely a race between two different responses and may not be a bug. Nonetheless, here is a report to track it if you want to consider changing the behavior in the future.)

On linux x64, the following program will go test -v with "Failed 0 out of 100" with go1.4. In go1.5beta1, I often get results like

...
http_test.go:33: 98 Put http://localhost:8080/: write tcp 127.0.0.1:50310->127.0.0.1:8080: write: connection reset by peer
http_test.go:38: Failed 68 out of 100

This seems to be caused by commit 1045351 and reverting that commit makes the test pass again.

package main

import (
        "net/http"
        "strings"
        "testing"
)

const contentLengthLimit = 1024 * 1024 // 1MB                                                                                                                             

func handler(w http.ResponseWriter, r *http.Request) {
        if r.ContentLength >= contentLengthLimit {
                w.WriteHeader(http.StatusBadRequest)
                r.Body.Close()
                return
        }
        w.WriteHeader(http.StatusOK)
}

func TestHandler(t *testing.T) {
        http.HandleFunc("/", handler)
        go http.ListenAndServe(":8080", nil)

        fail := 0
        count := 100
        for i := 0; i < count; i++ {
                r := strings.NewReader(strings.Repeat("a", 10000000))
                req, err := http.NewRequest("PUT", "http://localhost:8080/", r)
                client := &http.Client{}
                resp, err := client.Do(req)
                if err != nil {
                        fail++
                        t.Log(i, err)
                        //t.Fatal(err)                                                                                                                                    
                } else if resp.StatusCode != 400 {
                        t.Fatalf("Expected the status 400, got %v instead.", resp.StatusCode)
                }
        }
        t.Logf("Failed %v out of %v\n", fail, count)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Thinking

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions