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

Closed
soypat opened this issue Jan 27, 2022 · 6 comments · May be fixed by #65327
Closed

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

soypat opened this issue Jan 27, 2022 · 6 comments · May be fixed by #65327
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

yunginnanet added a commit to yunginnanet/go that referenced this issue Oct 8, 2023
Signed-off-by: Yung Innanet <kayos@smoqueed.com>
Google CLA: kayos@smoqueed.com
yunginnanet added a commit to yunginnanet/go that referenced this issue Oct 8, 2023
@gopherbot
Copy link

Change https://go.dev/cl/533119 mentions this issue: net/http: wrap client errors

@cherrymui
Copy link
Member

Reopen. CL reverted.

@gopherbot
Copy link

Change https://go.dev/cl/559035 mentions this issue: net/http: wrap client errors

ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
Fixes golang#50856

Change-Id: I7fe89fcce223e1571debb73436f8aeb3bfbe4b9f
GitHub-Last-Rev: be570e7
GitHub-Pull-Request: golang#63448
Reviewed-on: https://go-review.googlesource.com/c/go/+/533119
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
@gopherbot
Copy link

Change https://go.dev/cl/567537 mentions this issue: net/http: make timeout errors match context.DeadlineExceeded

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

Successfully merging a pull request may close this issue.

4 participants