http.Client stops working after connected to VPN

516 views
Skip to first unread message

Diep Pham

unread,
Nov 17, 2016, 9:52:54 AM11/17/16
to golang-nuts
I have a program that periodically does a HTTP GET. The simplified codes
looks like that.

```
package main

import (
"log"
"net/http"
"testing"
"time"
)

func TestPlay(t *testing.T) {
for {
log.Println("starting request")
// If I use this, the test will hang forever!
// rsp, err := http.Get("http://google.com")
client := http.Client{Timeout: time.Second * 3}
rsp, err := client.Get("http://google.com")
if err != nil {
t.Fatal(err)
}
if err = rsp.Body.Close(); err != nil {
t.Fatal(err)
}
log.Println("request finished")
log.Println("sleeping...")
time.Sleep(time.Second * 1)
}
}
```



Problem is when a user connects to a VPN network, the request will fails
with following error.

```
main_test.go:17: Get http://google.com: net/http: request canceled
(Client.Timeout exceeded while awaiting headers)
```

And if I didn't set timeout in http.Client, it will hang forever!

So my questions are:
- is this normal, expected behavior of http.Client?
- what is the best way to handle this? beside just retry the request?

Tamás Gulácsi

unread,
Nov 18, 2016, 6:14:01 AM11/18/16
to golang-nuts, m...@favadi.com


Most probably the network is misconfigured, or just does not allow access to http://google.com through the VPN.
Either allow accessing google.com through the VPN, or configure the network to only "use the resources" on the VPN, do not forward all traffic through it.

This is not a Go problem, though.

Diep Pham

unread,
Nov 18, 2016, 6:25:15 AM11/18/16
to Tamás Gulácsi, golang-nuts
Tamás Gulácsi wrote:
> Either allow accessing google.com through the VPN, or configure the
> network to only "use the resources" on the VPN, do not forward all
> traffic through it.

Maybe I didn't describe the problem clearly. VPN configuration is OK, I
can connect to Google normally. Only the HTTP request happens right
after VPN connection (or maybe during VPN connection) fails. If I use
default HTTP client, it will freeze forever. If I set timeout to 10s, it
will hang for 10s and returns the "net/http: request canceled
(Client.Timeout exceeded while awaiting headers" error.

Tamás Gulácsi

unread,
Nov 18, 2016, 6:48:07 AM11/18/16
to golang-nuts, tgula...@gmail.com, m...@favadi.com

In this casee you should just retry: the VPN transition may mean that the previously resolved DNS address of google.com has changed, or the whole routing has changed, and you have to start over.

Diep Pham

unread,
Nov 18, 2016, 8:12:25 AM11/18/16
to Tamás Gulácsi, golang-nuts
Tamás Gulácsi wrote:
> In this casee you should just retry: the VPN transition may mean that
> the previously resolved DNS address of google.com has changed, or the
> whole routing has changed, and you have to start over.

Well, the program do many sequential HTTP calls. I guess I will have to
wrap them all in a retry-n-number-of-times function. But is it expected
behavior of a HTTP client when network change?
Reply all
Reply to author
Forward
0 new messages