Autocert failure with HTTP handler

81 views
Skip to first unread message

cpu...@gmail.com

unread,
Apr 19, 2021, 3:20:13 PM4/19/21
to golang-nuts
Hello, 

I have a very stupid problem with autocert. The below code works:

log.Println("sslHosts:", sslHosts)
log.Println("sslCertDir:", sslCertDir)

certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(strings.Split(sslHosts, " ")...),
Cache: autocert.DirCache(sslCertDir),
}

go func() {
log.Fatal(http.ListenAndServe(":http", certManager.HTTPHandler(nil)))
}()
// go log.Fatal(http.ListenAndServe(":http", certManager.HTTPHandler(nil)))

s.Addr = ":https"
s.TLSConfig = certManager.TLSConfig()

log.Fatal(s.ListenAndServeTLS("", ""))

Once I remove the go func() and uncomment the // go log line, the server no longer serves HTTPS requests while HTTP continues to work. 

I'm totally stumped- the two go funcs should imho be equivalent? Any insight what's wrong here would be highly appreciated. 

I'm using go 1.16.3-arm64 on Darwin, cross-compiled to amd64.

Cheers,
Andi

Brian Candler

unread,
Apr 19, 2021, 5:29:31 PM4/19/21
to golang-nuts
As far as I understand it: "go foo(bar, baz)" evaluates all the arguments first, *then* starts a goroutine with function foo(val1, val2)

Since http.ListenAndServe() never returns [unless it fails to start], it never gets as far as starting a goroutine for log.Fatal, and therefore cannot continue to the rest of the code.

cpu...@gmail.com

unread,
Apr 19, 2021, 5:39:44 PM4/19/21
to golang-nuts
That makes perfect sense as the simple reproducer shows: https://play.golang.org/p/vSEBtdV2CzR

Only in this case I didn't see it as there were more go routines alive.

Much appreciated, thank you!

Brian Candler

unread,
Apr 20, 2021, 2:58:46 AM4/20/21
to golang-nuts
Another way to look at it:
Reply all
Reply to author
Forward
0 new messages