Description

Connection reset by peer always occurred after benchmark.

How to reproduce

Refer to the source for wrk benchmark

https://github.com/PacktPublishing/Building-Distributed-Applications-in-Gin/tree/main/chapter07/api-without-db

Change this line gin.Default() to gin.New()

go mod init gintest
go mod tidy
GIN_MODE=release ./e

Expectations

Benchmark should return no stack error

wrk -c 100 http://localhost:8080/recipes

Large JSON response will retrieve from /recipes endpoint.

Actual result

Error stack will throw right after the benchmark finished.

2022/10/04 22:55:10 http: panic serving [::1]:57818: write tcp [::1]:8080->[::1]:57818: write: connection reset by peer
goroutine 30 [running]:
net/http.(*conn).serve.func1()
    /usr/local/go/src/net/http/server.go:1850 +0xb0
panic({0x104c6a5c0, 0x14000530050})
    /usr/local/go/src/runtime/panic.go:890 +0x258
github.com/gin-gonic/gin/render.JSON.Render(...)
    /Users/s/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/render/json.go:58
github.com/gin-gonic/gin.(*Context).Render(0x14000513d00, 0xc8, {0x104cbe180, 0x140005060b0})
    /Users/s/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:910 +0xf4
github.com/gin-gonic/gin.(*Context).JSON(...)
...

Environment

  • go version: 1.19.1
  • gin version (or commit ref): 1.8.1
  • operating system: macOS Ventura
  • hardware: Macbook Pro (M1)

Comment From: jamesli2021

So this caused panic: https://github.com/gin-gonic/gin/blob/master/render/json.go#L58

Comment From: ElegantSoft

Any updates

Comment From: mimo-x

so how to solve this problem······🤔

Comment From: ElegantSoft

The problem was in nginx

Comment From: jamesli2021

The problem was in nginx

Are you referring to your setup and same codebase?

Comment From: jamesli2021

It's the problem with the panic, happen without nginx when you change to router.Run(":3000") or random ports.

Test command: wrk http://localhost:3000/recipes

For full info on errors:

[GIN-debug] Listening and serving HTTP on :3000
2022/10/18 19:52:34 http: panic serving [::1]:63713: write tcp [::1]:3000->[::1]:63713: write: connection reset by peer
goroutine 7 [running]:
net/http.(*conn).serve.func1()
    /usr/local/go/src/net/http/server.go:1850 +0xb0
panic({0x104e32700, 0x1400018e140})
    /usr/local/go/src/runtime/panic.go:890 +0x258
github.com/gin-gonic/gin/render.JSON.Render(...)
    /Users/s/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/render/json.go:58
github.com/gin-gonic/gin.(*Context).Render(0x1400069c500, 0xc8, {0x104e86420, 0x14000094100})
    /Users/s/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:910 +0xf4
github.com/gin-gonic/gin.(*Context).JSON(...)
    /Users/s/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:953
main.ListRecipesHandler(0x12c79b8b8?)
    /Users/s/Downloads/Building-Distributed-Applications-in-Gin-main/chapter07/api-without-db/main.go:55 +0x74
github.com/gin-gonic/gin.(*Context).Next(...)
    /Users/s/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0x140000829c0, 0x1400069c500)
    /Users/s/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/gin.go:616 +0x554
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0x140000829c0, {0x104e86bd0?, 0x140004420e0}, 0x14000252000)
    /Users/s/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/gin.go:572 +0x1d4
net/http.serverHandler.ServeHTTP({0x14000090750?}, {0x104e86bd0, 0x140004420e0}, 0x14000252000)
    /usr/local/go/src/net/http/server.go:2947 +0x2c4
net/http.(*conn).serve(0x140004b23c0, {0x104e87380, 0x14000090510})
    /usr/local/go/src/net/http/server.go:1991 +0x560
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:3102 +0x444

Comment From: daniel-res

@ElegantSoft also having this issue, using NGINX as well. were you able to fix it?