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
Cancel request by client will cause panic #2279
Comments
I had solve it. Don't use the default recover middleware and define a new one like this. func RequestCancelRecover() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
fmt.Println("client cancel the request")
c.Request.Context().Done()
}
}()
c.Next()
}
} use in the code for example: r := gin.New()
r.Use(gin.Logger(), RequestCancelRecover())
r.GET("/", controller.Index)
r.Run(":8000") |
This was referenced Jan 6, 2023
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
Panic error if client cancel request when server is running a long task and response >8192 length content after it.
(content length <=8192 works fine.)
Are there any other solutions except above??
How to reproduce
1.Open the page directly in the browser then close the tab before server response
2.Send request in Postman then cancel it before server response
Expectations
ignore the request if it's cancelled
Actual result
GET / HTTP/1.1
Host: 127.0.0.1:8080
Connection: close
Accept: /
Accept-Encoding: gzip, deflate, br
Cache-Control: no-cache
Connection: close
Postman-Token: c137d141-a132-49ee-8087-397f5ca7d131
User-Agent: PostmanRuntime/7.22.0
write tcp 127.0.0.1:8080->127.0.0.1:54928: wsasend: An established connection was aborted by the software in your host machine.
/src/github.com/gin-gonic/gin/context.go:817 (0x8ed689)
(*Context).Render: panic(err)
/src/github.com/gin-gonic/gin/context.go:892 (0x8ff331)
(*Context).String: c.Render(code, render.String{Format: format, Data: values})
/tmp/main.go:13 (0x8ff2b5)
main.func1: c.String(200, strings.Repeat("A", 8193))
/src/github.com/gin-gonic/gin/context.go:147 (0x8e95d1)
(*Context).Next: c.handlersc.index
/src/github.com/gin-gonic/gin/recovery.go:83 (0x8fd86a)
RecoveryWithWriter.func1: c.Next()
/src/github.com/gin-gonic/gin/context.go:147 (0x8e95d1)
(*Context).Next: c.handlersc.index
/src/github.com/gin-gonic/gin/logger.go:241 (0x8fc977)
LoggerWithConfig.func1: c.Next()
/src/github.com/gin-gonic/gin/context.go:147 (0x8e95d1)
(*Context).Next: c.handlersc.index
/src/github.com/gin-gonic/gin/gin.go:411 (0x8f3b23)
(*Engine).handleHTTPRequest: c.Next()
/src/github.com/gin-gonic/gin/gin.go:369 (0x8f3214)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/src/net/http/server.go:2802 (0x6bab8a)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/src/net/http/server.go:1890 (0x6b635b)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/src/runtime/asm_amd64.s:1357 (0x45add0)
goexit: BYTE $0x90 // NOP
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 200 with 500
Environment
The text was updated successfully, but these errors were encountered: