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: TLS server on a goroutine won't respond to clients on different goroutines #3220

Closed
gopherbot opened this issue Mar 6, 2012 · 1 comment

Comments

@gopherbot
Copy link

by elazarl:

To recreate the problem:

$ mkdir $GOPATH/httpstest
$ cd $GOPATH/httpstest
$ cat ->https_test.go
package main

import ("net/http"
        "crypto/tls"
        "testing")

func init() {
        go http.ListenAndServeTLS(":10122", "cert.pem", "key.pem", nil)
}

func TestHttps(t *testing.T) {
        var acceptAllCerts = &tls.Config{InsecureSkipVerify: true}
        client := &http.Client{Transport:&http.Transport{TLSClientConfig:acceptAllCerts}}
        _,err := client.Get("https://localhost:10122/bobo";)
        if err != nil {
                t.Error("can't connect to server",err)
        }
}

$ go test
--- FAIL: TestHttps (0.05 seconds)
        https_test.go:16: can't connect to server Get https://localhost:10122/bobo: dial tcp 127.0.0.1:10122: connection refused
FAIL
exit status 1
FAIL    _/home/vmplanet/gopath/testhttps        0.058s

Other processes can access the http-TLS server.

$ hg id
f4470a54e6db weekly/weekly.2012-03-04

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 11.04
Release:        11.04
Codename:       natty


$ uname -a
Linux ubuntu-vm 2.6.38-10-generic #46-Ubuntu SMP Tue Jun 28 15:05:41 UTC 2011 i686 i686
i386 GNU/Linux
@bradfitz
Copy link
Contributor

bradfitz commented Mar 6, 2012

Comment 1:

I think this is just a race.  There's no guarantee that ListenAndServeTLS will be
running by the time client.Get runs.
init runs first, but the goroutine that it starts isn't guaranteed to start running
before TestHttps.
I bet if you add a time.Sleep at the beginning of TestHttps it "works", but that's a
hacky solution.
The better solution is to start listening first with a new net.Listener, then start a
serving goroutine (using the listener you already started), and *then* make your HTTP
request.
Or just use:
http://weekly.golang.org/pkg/net/http/httptest/#NewTLSServer

Status changed to Invalid.

@mikioh mikioh changed the title TLS server on a goroutine won't respond to clients on different goroutines net/http: TLS server on a goroutine won't respond to clients on different goroutines Jan 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

2 participants