Go: net/http: Client.Do https request wrapped in goroutine hangs

Created on 26 Apr 2018  路  5Comments  路  Source: golang/go

What version of Go are you using (go version)?

go version go1.10.1 linux/amd64

What operating system and processor architecture are you using (go env)?

Linux amk 4.16.3-1-ARCH #1 SMP PREEMPT Thu Apr 19 09:17:56 UTC 2018 x86_64 GNU/Linux

GOARCH="amd64"
GOBIN=""
GOCACHE="/usr/home/am/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/usr/home/am/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build722852943=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I made and run this script via go run test.go:

package main

import (
    "bytes"
    "context"
    "io/ioutil"
    "log"
    "net/http"
    "time"
)

// TestRequest ...
func TestRequest(url string, m string) {
    req, err := http.NewRequest("GET", url, bytes.NewBufferString(m))
    req.Header.Set("Content-Type", "application/json")

    ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer ctxCancel()
    req = req.WithContext(ctx)

    log.Println("Starting default client")
    resp, err := http.DefaultClient.Do(req)
    log.Println("Finished default client", resp, err)
    if err != nil {
        log.Println(err)
    }
    defer resp.Body.Close()

    body, _ := ioutil.ReadAll(resp.Body)
    log.Println(string(body))
}

// Worker ...
func Worker() {
    TestRequest("https://google.com", "{}")
    TestRequest("http://google.com", "{}")
}

func main() {
    go Worker()
    for {
    }
}

What did you expect to see?

All requests to be performed.
In standard output something like this:

Starting default client
Finished default client <sample body> <sample error>
Starting default client
Finished default client <sample body> <sample error>

OR panic (some messages via stderr)

What did you see instead?

None requests were performed, no output except first line.
In standard output:

Starting default client

In debugger (vscode + delve) it hangs on line 171 of client.go:

func (c *Client) send(req *Request, deadline time.Time) (resp *Response, didTimeout func() bool, err error) {
>>  if c.Jar != nil {
        for _, cookie := range c.Jar.Cookies(req.URL) {

Here c is defined and c.Jar is nil.

Additional

I tested it against http without ssl and it works fine.

FrozenDueToAge

Most helpful comment

It's due to the for {} busy loop. If you want to block forever a goroutine use select {} instead.

All 5 comments

Changed url to yandex.ru and now it works fine, perhaps it happened due blocks, but still I don't understand why this method didn't fail or output something.

Duplicate of #25054

It's due to the for {} busy loop. If you want to block forever a goroutine use select {} instead.

@FiloSottile okay, I confirm this, thank you.

Was this page helpful?
0 / 5 - 0 ratings