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: HttpClient does not close connection after timeout #14335

Closed
janisz opened this issue Feb 15, 2016 · 3 comments
Closed

net/http: HttpClient does not close connection after timeout #14335

janisz opened this issue Feb 15, 2016 · 3 comments

Comments

@janisz
Copy link

janisz commented Feb 15, 2016

Hi

I experienced a problem with HttpClient. For following code, I expect to have some opened TCP connections, but usually it's over 700.

package main

import (
    "io"
    "io/ioutil"
    "net/http"
    "fmt"
)

func main() {

    client := http.DefaultClient
    client.Timeout = 1
    fmt.Println("Sending requests:")
    for i := 0; i < 1000; i++ {
        resp, err := client.Get("http://google.com/")
        fmt.Print(".")
        if err == nil {
            fmt.Println("\nClose response")
            io.Copy(ioutil.Discard, resp.Body)
            resp.Body.Close()
        }
    }
    fmt.Println("\nWaiting")
    for {}
}

I tried changing configuration of http client (DisableKeepAlives, MaxIdleConnsPerHost etc) but always ended up with many open connections.

Similar issue was mentioned here:

Tested on:

  • go version go1.5 darwin/amd64
  • go version go1.5 linux/amd64
  • go version go1.6rc2 linux/amd64

CC: @dankraw

@cespare
Copy link
Contributor

cespare commented Feb 15, 2016

cc @bradfitz

@mjarco
Copy link

mjarco commented Feb 15, 2016

You've set timeout for one nanosecond and then blocked main goroutine. So none of your requests were send but you initiated some syscalls, and didn't give any time for transport/dialer to shut down connections. That's why they hang infinitely.
I would not consider it as a bug.
Some code that proofs my theory:
https://play.golang.org/p/uGxH_OpBMX

@ianlancetaylor ianlancetaylor changed the title HttpClient does not close connection after timeout net/http: HttpClient does not close connection after timeout Feb 16, 2016
@ianlancetaylor
Copy link
Contributor

Don't write for {}. Write select {}.

Please feel free to reopen if you disagree.

@golang golang locked and limited conversation to collaborators Feb 28, 2017
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