Skip to content

net/context: ctx.Err() != error returned by ctxhttp.Do #16381

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

Closed
abhinav opened this issue Jul 15, 2016 · 2 comments
Closed

net/context: ctx.Err() != error returned by ctxhttp.Do #16381

abhinav opened this issue Jul 15, 2016 · 2 comments

Comments

@abhinav
Copy link
Contributor

abhinav commented Jul 15, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?

    go version go1.7rc1 darwin/amd64
    
  2. What operating system and processor architecture are you using (go env)?

    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="darwin"
    GOOS="darwin"
    GOPATH="/Users/abg/dev/go"
    GORACE=""
    GOROOT="/Users/abg/.gimme/versions/go1.7rc1.darwin.amd64"
    GOTOOLDIR="/Users/abg/.gimme/versions/go1.7rc1.darwin.amd64/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-build692583142=/tmp/go-build -gno-record-gcc-switches -fno-common"
    CXX="clang++"
    CGO_ENABLED="1"
    
  3. What did you do?

    The contract for ctxhttp.Do states that if a request timed out or got
    canceled, the error returned will be equal to ctx.Err(). This is true
    for Go 1.6 but is broken in 1.7rc1.

    I wrote a quick little program where the request always times out. It
    prints the returned error, the ctx.Err(), and whether they are equal.

    Full code:

    package main
    
    import (
        "fmt"
        "log"
        "net/http"
        "time"
    
        "golang.org/x/net/context"
        "golang.org/x/net/context/ctxhttp"
    )
    
    func main() {
        http.HandleFunc("/", func(http.ResponseWriter, *http.Request) {
            time.Sleep(1 * time.Second)
        })
        go http.ListenAndServe(":8080", nil)
    
        request, err := http.NewRequest("GET", "http://localhost:8080", nil)
        if err != nil {
            log.Fatal(err)
        }
    
        ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
        defer cancel()
    
        _, err = ctxhttp.Do(ctx, nil, request)
        if err == nil {
            log.Fatal("expected error")
        }
    
        fmt.Println("err =", err)
        fmt.Println("ctx.Err() =", ctx.Err())
        fmt.Println("err == ctx.Err() =", err == ctx.Err())
    }
    
  4. What did you expect to see?

    Expected output:

    err = context deadline exceeded
    ctx.Err() = context deadline exceeded
    err == ctx.Err() = true
    

    This works as expected in 1.6.

  5. What did you see instead?

    Output in 1.7rc1:

    err = Get http://localhost:8080: net/http: request canceled
    ctx.Err() = context deadline exceeded
    err == ctx.Err() = false
    

    This fails even if net/context is switched to Go 1.7's context package.

@ianlancetaylor ianlancetaylor added this to the Go1.7Maybe milestone Jul 15, 2016
@gopherbot
Copy link
Contributor

CL https://golang.org/cl/24977 mentions this issue.

@gopherbot
Copy link
Contributor

CL https://golang.org/cl/24978 mentions this issue.

gopherbot pushed a commit that referenced this issue Aug 23, 2016
This permits the error message to distinguish between a context that was
canceled and a context that timed out.

Updates #16381.

Change-Id: I3994b98e32952abcd7ddb5fee08fa1535999be6d
Reviewed-on: https://go-review.googlesource.com/24978
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
@golang golang locked and limited conversation to collaborators Jul 16, 2017
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