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: Client does not wrap context errors #50856

Open
soypat opened this issue Jan 27, 2022 · 2 comments
Open

net/http: Client does not wrap context errors #50856

soypat opened this issue Jan 27, 2022 · 2 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@soypat
Copy link

soypat commented Jan 27, 2022

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

$ go version
go version go1.17.3 linux/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/pato/go/bin"
GOCACHE="/home/pato/.cache/go-build"
GOENV="/home/pato/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/pato/go/pkg/mod"
GONOPROXY="github.com/LIA-Aerospace"
GONOSUMDB="github.com/LIA-Aerospace"
GOOS="linux"
GOPATH="/home/pato/go"
GOPRIVATE="github.com/LIA-Aerospace"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/pato/src/lia/gptos/_gui/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3992144195=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Issue not reproducible on go playground.
https://go.dev/play/p/NfEhKnRBFuv

// You can edit this code!
// Click here and start typing.
package main

import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"time"
)

func main() {
	client := http.Client{
		Timeout: 1 * time.Millisecond,
	}
	b, err := json.Marshal(&struct{}{})
	if err != nil {
		panic(err)
	}
	_, err = client.Post("http://google.com", "application/json; charset=UTF-8", bytes.NewBuffer(b))
	if err != nil {
		switch {
		case errors.Is(err, context.DeadlineExceeded):
			fmt.Println("server took too long!") // Expected output
			return
		}
		panic("unexpected:" + err.Error())
	}
	fmt.Println("all good :)")
}

What did you expect to see?

server took too long!

What did you see instead?

panic: unexpected:Post "http://google.com": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

goroutine 1 [running]:
main.main()
        /home/pato/src/test/main.go:29 +0x1ac
exit status 2

This is because the errors are wrapped like this:

(type:*url.Error):Post "http://google.com": context deadline exceeded (Client.Timeout exceeded while awaiting headers)(type:*http.httpError):context deadline exceeded (Client.Timeout exceeded while awaiting headers)

Second error could easily wrap context.DeadlineExceeded

@mknyszek mknyszek changed the title http: Client does not wrap context errors net/http: Client does not wrap context errors Jan 27, 2022
@mknyszek
Copy link
Contributor

CC @neild

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 27, 2022
@mknyszek mknyszek added this to the Backlog milestone Jan 27, 2022
@gopherbot
Copy link

Change https://golang.org/cl/381694 mentions this issue: net/http: Client wraps context errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants