Closed
Description
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version
)?
$ go version
go version go1.7.3 darwin/amd64
What operating system and processor architecture are you using (go env
)?
$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/abg/dev/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.7.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.7.3/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8z/qdzjsr3n5l72vdg67zr6xkjc0000gn/T/go-build965147697=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
What did you do?
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
)
func main() {
req, err := http.NewRequest("GET", "https://httpbin.org/delay/3", nil)
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
_, err = http.DefaultClient.Do(req.WithContext(ctx))
fmt.Printf("err = %v (%T)\n", err, err)
fmt.Printf("ctx.Err() = %v (%T)\n", ctx.Err(), ctx.Err())
}
What did you expect to see?
err = context deadline exceeded (context.deadlineExceededError)
ctx.Err() = context deadline exceeded (context.deadlineExceededError)
err
should be a "context deadline exceeded" error and it should be the same as ctx.Error()
.
What did you see instead?
err
and ctx.Error()
are different. err
is a "request canceled" error even though the context's cancel()
hasn't been called yet.
err = Get https://httpbin.org/delay/3: net/http: request canceled while waiting for connection (*url.Error)
ctx.Err() = context deadline exceeded (context.deadlineExceededError)
Related to #16381, which was addressed in ctxhttp
using,
if err != nil {
select {
case <-ctx.Done():
err = ctx.Err()
default:
}
}