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.Do https request wrapped in goroutine hangs #25099

Closed
am-trinity opened this issue Apr 26, 2018 · 5 comments
Closed

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

am-trinity opened this issue Apr 26, 2018 · 5 comments

Comments

@am-trinity
Copy link

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.

@am-trinity
Copy link
Author

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.

@am-trinity
Copy link
Author

am-trinity commented Apr 26, 2018

Maybe related #13957 #16582

@FiloSottile
Copy link
Contributor

Duplicate of #25054

@FiloSottile FiloSottile marked this as a duplicate of #25054 Apr 26, 2018
@FiloSottile
Copy link
Contributor

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

@am-trinity
Copy link
Author

@FiloSottile okay, I confirm this, thank you.

@golang golang locked and limited conversation to collaborators Apr 27, 2019
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

3 participants