Skip to content

net/http: write error from swiftly dropped connection #3595

Closed
@rogpeppe

Description

@rogpeppe
The following program prints:

got err: <nil>
got body: "the bar is closed\n"
got err: write tcp 127.0.0.1:8080: connection reset by peer

I think the second error is a bug - the http package
isn't waiting for the response before printing the
error from writing to the server.

package main

import (
    "bytes"
    "fmt"
    "io"
    "io/ioutil"
    "log"
    "net/http"
    "strings"
)

func main() {
    http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
        http.Error(w, "the bar is closed", 404)
    })
    go http.ListenAndServe(":8080", nil)
    post(strings.NewReader("hello"))
    post(bytes.NewBuffer(make([]byte, 4e6)))
}

func post(r io.Reader) {
    resp, err := http.Post("http://localhost:8080/bar";, "text/utf8", r)
    fmt.Printf("got err: %v\n", err)
    if resp != nil {
        data, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("got body: %q\n", data)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions