Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: Client can't close HTTP stream. #3672

Closed
dustin opened this issue May 28, 2012 · 6 comments
Closed

net/http: Client can't close HTTP stream. #3672

dustin opened this issue May 28, 2012 · 6 comments
Milestone

Comments

@dustin
Copy link

dustin commented May 28, 2012

I can't seem to close an HTTP response body while it's still sending data.  I ran into
this in an app I was writing, but I believe the following client and server demonstrate
the problem (sorry, not sure how to write a trustworthy test for this):


// server.go:

package main

import (
    "log"
    "net/http"
    "time"
)

func foreverYoung(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-type", "application/javascript")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.WriteHeader(200)

    for {
        w.Write([]byte("young\r\n"))
        w.(http.Flusher).Flush()
        time.Sleep(time.Second)
    }
}

func main() {
    http.HandleFunc("/", foreverYoung)
    log.Fatal(http.ListenAndServe(":4334", nil))
}




// client.go:

package main

import (
    "log"
    "net/http"
    "os"
)

func main() {
    resp, err := http.Get("http://localhost:4334/";)
    if err == nil {
        func() {
            defer resp.Body.Close()
            for {
                b := []byte{0}
                _, err := resp.Body.Read(b)
                if err != nil {
                    log.Fatalf("Error reading stuff: %v", err)
                }
                os.Stdout.Write(b)
                if b[0] == byte('\n') {
                    return
                }
            }
        }()
    } else {
        log.Fatalf("Error in stream: %v", err)
    }
}
@bradfitz
Copy link
Contributor

Comment 1:

Labels changed: removed priority-triage.

Owner changed to @bradfitz.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 2:

Labels changed: added go1.1.

@shanemhansen
Copy link
Contributor

Comment 3:

So, it seems like the right thing to do here is eliminate the call to
io.Copy(ioutil.Discard, b) in transfer.go, right?
Seems like the only reasonable alternative is to set a timeout and return from Close if
the discard operation doesn't complete in some reasonable amount of time.
I understand that failing to consume a response body would be really bad, esp wrt to
connection pooling, so it's tough to tell what the right thing to do here is.

@rsc
Copy link
Contributor

rsc commented Dec 10, 2012

Comment 4:

Labels changed: added size-l.

@bradfitz
Copy link
Contributor

bradfitz commented Mar 4, 2013

Comment 5:

I've mailed out https://golang.org/cl/7419050
Please test it.

Status changed to Started.

@bradfitz
Copy link
Contributor

bradfitz commented Mar 6, 2013

Comment 6:

This issue was closed by revision ce83415.

Status changed to Fixed.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants