Skip to content

net/http: HTTP large file upload fails with "bufio: buffer full" #26707

Closed
@jvehent

Description

@jvehent

Uploading large files in HTTP multipart POST requests fails with multipart: NextPart: bufio: buffer full.

What version of Go are you using (go version)?

go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ulfr/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/ulfr"
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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-build508991886=/tmp/go-build -gno-record-gcc-switches"

What did you do?

The following source code accepts uploads requests over HTTP. It works fine for files up to 15GB, but crashes for 20GB files with the error: multipart: NextPart: bufio: buffer full

package main

import (
        "log"
        "net/http"
)

func main() {
        http.HandleFunc("/upload", uploadHandler)
        http.ListenAndServe(":5050", nil)
}

func uploadHandler(w http.ResponseWriter, r *http.Request) {
        log.Printf("%s %s %s user-agent=%s", r.Method, r.Proto, r.URL.String(), r.UserAgent())
        if r.Method != http.MethodPost {
                http.Error(w, "only POST method is allowed", http.StatusBadRequest)
                return
        }
        log.Printf("parsing multipart form")
        // no more than 100MB of memory, the rest goes into /tmp
        r.ParseMultipartForm(100000000)
        file, handler, err := r.FormFile("uploadfile")
        if err != nil {
                http.Error(w, "failed to read uploadfile form field", http.StatusBadRequest)
                log.Printf("failed to read uploadfile form field: %v", err)
                return
        }
        defer file.Close()
        log.Println(handler.Filename)
}

Here's an example run with a 20GB file filed with zeroes.

$ go run multipart.go 
2018/07/31 08:29:18 POST HTTP/1.1 /upload user-agent=curl/7.59.0
2018/07/31 08:29:18 parsing multipart form
2018/07/31 08:29:43 failed to read uploadfile form field: multipart: NextPart: bufio: buffer full

upload with curl:

$ curl -vi -F uploadfile="@/home/ulfr/20gfile.bin" http://127.0.0.1:5050/upload
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5050 (#0)
> POST /upload HTTP/1.1
> Host: 127.0.0.1:5050
> User-Agent: curl/7.59.0
> Accept: */*
> Content-Length: 20971520209
> Content-Type: multipart/form-data; boundary=------------------------879658a7528de349
> Expect: 100-continue
> 

< HTTP/1.1 100 Continue

< HTTP/1.1 400 Bad Request
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Tue, 31 Jul 2018 12:29:43 GMT
< Content-Length: 37
< Connection: close
< 
failed to read uploadfile form field

* we are done reading and this is set to close, stop send
* Closing connection 0

Since my machine has 32GB of RAM, I tried boosting MAXMEMORY to 30GB by setting r.ParseMultipartForm(3000000000) but got the same results.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions