gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
77.99k stars 7.97k forks source link

An error occurred when uploading files was canceled #3439

Open 814704261 opened 1 year ago

814704261 commented 1 year ago

Description

This code works fine. However, an error will be reported when the user cancels the upload in the middle of the upload

How to reproduce

func main() {
    r := gin.Default()

    r.POST("/upload", func(ctx *gin.Context) {
        file, _ := ctx.FormFile("file")
        log.Println(file.Size)
        ctx.String(http.StatusOK, "success")
    })

    r.Run(":3000")
}

Expectations

2022/12/16 15:33:21 759694258

Actual result

2022/12/16 15:17:28 [Recovery] 2022/12/16 - 15:17:28 panic recovered:
POST /upload HTTP/1.1
Host: localhost:3000
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 759694630
Content-Type: multipart/form-data; boundary=--------------------------876112661567559488104173
Postman-Token: dbf6730e-a932-47cf-b822-e954d1bc7c36
User-Agent: PostmanRuntime/7.29.0

runtime error: invalid memory address or nil pointer dereference
C:/Program Files/Go/src/runtime/panic.go:220 (0x829835)
        panicmem: panic(memoryError)
C:/Program Files/Go/src/runtime/signal_windows.go:255 (0x829805)
        sigpanic: panicmem()
D:/新建文件夹/断点续传/main.go:18 (0xb13550)
        main.func1: log.Println(file.Size)
C:/Users/demon/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xb0d961)
        (*Context).Next: c.handlers[c.index](c)
C:/Users/demon/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/recovery.go:101 (0xb0d94c)
        CustomRecoveryWithWriter.func1: c.Next()
C:/Users/demon/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xb0ca46)
        (*Context).Next: c.handlers[c.index](c)
C:/Users/demon/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/logger.go:240 (0xb0ca29)
        LoggerWithConfig.func1: c.Next()
C:/Users/demon/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xb0bb50)
        (*Context).Next: c.handlers[c.index](c)
C:/Users/demon/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/gin.go:616 (0xb0b7b8)
        (*Engine).handleHTTPRequest: c.Next()
C:/Users/demon/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/gin.go:572 (0xb0b47c)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
C:/Program Files/Go/src/net/http/server.go:2916 (0x9f3bfa)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
C:/Program Files/Go/src/net/http/server.go:1966 (0x9f0196)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
C:/Program Files/Go/src/runtime/asm_amd64.s:1571 (0x8438e0)
        goexit: BYTE    $0x90   // NOP

[GIN] 2022/12/16 - 15:17:28 | 500 |    1.6736426s |             ::1 | POST     "/upload"

Environment

814704261 commented 1 year ago

error message code postman tool