Skip to content

net/http: panic in net/http.(*chunkWriter).Write via bufio.Writer #61172

Closed as not planned
@krkeshav

Description

@krkeshav

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

$ go version
go version go1.20.5 linux/amd64

Does this issue reproduce with the latest release?

Not reproducable

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

go env Output
$ go env
GOOS="linux"
GOARCH="amd64"

What did you do?

Recently we did 2 things in our production application.

  1. We upgraded to Go 1.20.5 using golang:1.20 docker image
  2. We started heavily using io.Copy() function which has definition
func Copy(dst Writer, src Reader) (written int64, err error) {
	return copyBuffer(dst, src, nil)
}

Our use case for this function is basically we are getting response from some api.
It is a json response and the size of json can be very big (30MB to 50MB).
So instead of doing io.ReadAll and then json.Unmarshal and then sending bytes through writer we are
directly copying the response.Body to writer. This helps in saving CPU.

Here is the panic. Please note the panic was copied line by line from
AWS Cloudwatch so ordering may not be accurate for stacktrace.

http: panic serving 127.0.0.1:43680: runtime **error**: slice bounds out of range [4097:4096]
goroutine 324311 [running]:
net/http.(*conn).serve.func1()
    /usr/local/go/src/net/http/server.go:1854 +0xbf
panic({0x1693f60, 0xc001607db8})
    /usr/local/go/src/runtime/panic.go:890 +0x263
bufio.(*Writer).Write(0xc009525ec0, {0xc009fbab00?, 0x4?, 0xc003be9af8?})
    /usr/local/go/src/bufio/bufio.go:670 +0x1c8
fmt.Fprintf({0x1c35980, 0xc009525ec0}, {0x180c732, 0x4}, {0xc003be9af8, 0x1, 0x1})
    /usr/local/go/src/fmt/print.go:225 +0x9b
net/http.(*chunkWriter).Write(0xc003409620, {0xc003882000, 0x800, 0x800})
    /usr/local/go/src/net/http/server.go:382 +0xdb
bufio.(*Writer).Flush(0xc00a5bdc00)
    /usr/local/go/src/bufio/bufio.go:628 +0x62
net/http.(*response).finishRequest(0xc0034095e0)
    /usr/local/go/src/net/http/server.go:1649 +0x49
net/http.(*conn).serve(0xc0035ca5a0, {0x1c44ab0, 0xc0025acc00})
    /usr/local/go/src/net/http/server.go:2001 +0x65a
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:3089 +0x5ed
panic: runtime error: slice bounds out of range [4097:4096] [recovered]

panic: runtime error: slice bounds out of range [:4097] with capacity 4096
goroutine 324311 [running]:
bufio.(*Writer).Flush(0xc00242f7a0?)
    /usr/local/go/src/bufio/bufio.go:628 +0x185
net/http.(*conn).finalFlush(0xc0035ca5a0)
    /usr/local/go/src/net/http/server.go:1723 +0x73
net/http.(*conn).close(0xc0035ca5a0)
    /usr/local/go/src/net/http/server.go:1733 +0x1e
net/http.(*conn).serve.func1()
    /usr/local/go/src/net/http/server.go:1865 +0x1cc
panic({0x1693f60, 0xc001607db8})
    /usr/local/go/src/runtime/panic.go:890 +0x263
bufio.(*Writer).Write(0xc009525ec0, {0xc009fbab00?, 0x4?, 0xc003be9af8?})
    /usr/local/go/src/bufio/bufio.go:670 +0x1c8
fmt.Fprintf({0x1c35980, 0xc009525ec0}, {0x180c732, 0x4}, {0xc003be9af8, 0x1, 0x1})
    /usr/local/go/src/fmt/print.go:225 +0x9b
net/http.(*chunkWriter).Write(0xc003409620, {0xc003882000, 0x800, 0x800})
    /usr/local/go/src/net/http/server.go:382 +0xdb
bufio.(*Writer).Flush(0xc00a5bdc00)
    /usr/local/go/src/bufio/bufio.go:628 +0x62
net/http.(*response).finishRequest(0xc0034095e0)
    /usr/local/go/src/net/http/server.go:1649 +0x49
net/http.(*conn).serve(0xc0035ca5a0, {0x1c44ab0, 0xc0025acc00})
    /usr/local/go/src/net/http/server.go:2001 +0x65a
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:3089 +0x5ed

What did you expect to see?

We expected this panic to be caught so that application wont be down.
This is the first time we have seen this panic.

What did you see instead?

This panic is not caught and hence not recovered and our application crashed.

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.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions