imroc / req

Simple Go HTTP client with Black Magic
https://req.cool
MIT License
4.12k stars 334 forks source link

Panic on roundtripper #274

Closed adityathebe closed 10 months ago

adityathebe commented 10 months ago

I updated to v3.41.7 from v3.37.2 yesterday, since I was not able to build my old code with go 1.21. This is the exact code that was running before but now panics

client := req.C().SetUserAgent(userAgent).EnableForceHTTP3()
resp, err := client.R().Get(url)
if err != nil {
  return "", nil, err
}
defer resp.Body.Close()
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xa0799f]

goroutine 19 [running]:
github.com/imroc/req/v3/internal/http3.(*RoundTripper).getClient(0x0, {0xc00018a150, 0x12}, 0x0)
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/internal/http3/roundtrip.go:187 +0x5f
github.com/imroc/req/v3/internal/http3.(*RoundTripper).RoundTripOpt(0x0, 0xc0001be300, {0x0, 0x0})
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/internal/http3/roundtrip.go:136 +0xaf6
github.com/imroc/req/v3/internal/http3.(*RoundTripper).RoundTrip(0x0, 0xc0001be300)
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/internal/http3/roundtrip.go:160 +0x5d
github.com/imroc/req/v3.(*Transport).roundTrip(0xc000104ea0, 0xc0001be300)
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/transport.go:934 +0x5d6
github.com/imroc/req/v3.(*Transport).RoundTrip(0xc000104ea0, 0xc0001be300)
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/roundtrip.go:24 +0x11b
net/http.send(0xc0001be200, {0xbcea80, 0xc000104ea0}, {0xc131f44f38396264, 0x1bf1210129, 0xf56b00})
    /usr/lib/go/src/net/http/client.go:260 +0x4c2
net/http.(*Client).send(0xc0001e68a0, 0xc0001be200, {0xc131f44f38396264, 0x1bf1210129, 0xf56b00})
    /usr/lib/go/src/net/http/client.go:181 +0x15a
net/http.(*Client).do(0xc0001e68a0, 0xc0001be200)
    /usr/lib/go/src/net/http/client.go:724 +0x11ef
net/http.(*Client).Do(0xc0001e68a0, 0xc0001be200)
    /usr/lib/go/src/net/http/client.go:590 +0x49
github.com/imroc/req/v3.(*Client).roundTrip(0xc000149040, 0xc0001c0200)
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/client.go:1702 +0xac9
github.com/imroc/req/v3.(*Request).do(0xc0001c0200)
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/request.go:651 +0x473
github.com/imroc/req/v3.(*Request).Do(0xc0001c0200, {0x0, 0x0, 0x0})
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/request.go:619 +0x287
github.com/imroc/req/v3.(*Request).Send(0xc0001c0200, {0xb4530b, 0x3}, {0xb67b85, 0x35})
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/request.go:707 +0xcf
github.com/imroc/req/v3.(*Request).Get(0xc0001c0200, {0xb67b85, 0x35})
    /home/me/.local/share/go/pkg/mod/github.com/imroc/req/v3@v3.41.7/request.go:723 +0x71
github.com/adityathebe/my-project/services.(*MyClass)._getToken(0xc0001c23c0)
    /home/me/Projects/my-project/services/my_class.go:72 +0xd9
github.com/adityathebe/my-project/services.(*MyClass).login(0xc0001c23c0)
    /home/me/Projects/my-project/services/my_class.go:100 +0xf2
github.com/adityathebe/my-project/services.(*MyClass).makeAPICall(0xc0001c23c0, {0xc0001209f9, 0x5})
    /home/me/Projects/my-project/services/my_class.go:137 +0xbb
github.com/adityathebe/my-project/services.(*MyClass).Portfolio(0xc0001c23c0)
    /home/me/Projects/my-project/services/my_class.go:213 +0x9f
github.com/adityathebe/my-project/services.(*MyClass).PortfolioDailySummary(0xc0001c23c0, 0x1)
    /home/me/Projects/my-project/services/my_class.go:166 +0x89
main.main.func4()
    /home/me/Projects/my-project/cmd/cli/main.go:77 +0x5b
created by main.main in goroutine 1
    /home/me/Projects/my-project/cmd/cli/main.go:76 +0x6c5
imroc commented 10 months ago

I didn't reproduce that, can you share a reproducable code with that?

adityathebe commented 10 months ago

I didn't reproduce that, can you share a reproducable code with that?

package main

import (
    "fmt"
    "io"
    "log"

    "github.com/imroc/req/v3"
)

func main() {
    userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
    url := "https://adityathebe.com"

    client := req.C().SetUserAgent(userAgent).EnableForceHTTP3()
    resp, err := client.R().Get(url)
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()

    body, err := io.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(body)
}
go 1.21.0

require github.com/imroc/req/v3 v3.41.7
imroc commented 10 months ago

I didn't reproduce that, can you share a reproducable code with that?

package main

import (
  "fmt"
  "io"
  "log"

  "github.com/imroc/req/v3"
)

func main() {
  userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
  url := "https://adityathebe.com"

  client := req.C().SetUserAgent(userAgent).EnableForceHTTP3()
  resp, err := client.R().Get(url)
  if err != nil {
      log.Fatal(err)
  }
  defer resp.Body.Close()

  body, err := io.ReadAll(resp.Body)
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println(body)
}
go 1.21.0

require github.com/imroc/req/v3 v3.41.7

Still not reproduced.

adityathebe commented 10 months ago

That's weird. I've reproduced this in two different computers. Asked a friend and he could reproduce it as well.

Are you running go 1.21?

imroc commented 10 months ago

Oh I see, sorry that I used go 1.20 to test. Thanks for report, It's a bug, I have just fixed, and release a patch version, you can upgrade to the latest version.

adityathebe commented 10 months ago

Thanks. Works now 👍