Skip to content
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

panic on fasthttphandler #1469

Open
JayChoi1736 opened this issue Jan 9, 2023 · 5 comments
Open

panic on fasthttphandler #1469

JayChoi1736 opened this issue Jan 9, 2023 · 5 comments
Labels

Comments

@JayChoi1736
Copy link

JayChoi1736 commented Jan 9, 2023

i've got these panic error log.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0xf6a2bc]

goroutine 108517 [running]:
github.com/valyala/fasthttp.(*firstByteReader).Read(0xc0291b5908?, {0xc02a341fee?, 0x7f88c5c159a8?, 0x200?})
	/go/pkg/mod/github.com/valyala/[email protected]/server.go:834 +0x5c
bufio.(*Reader).Read(0xc04327a120, {0xc02a341fee, 0x1bd9, 0x1e386b0?})
	/usr/local/src/bufio/bufio.go:222 +0x106
github.com/valyala/fasthttp.(*requestStream).Read(0xc04d60c9c0, {0xc02a341fee?, 0xc0291b57b8?, 0x1e386b0?})
	/go/pkg/mod/github.com/valyala/[email protected]/streaming.go:75 +0x2e5
github.com/valyala/bytebufferpool.(*ByteBuffer).ReadFrom(0xc0299d9140, {0x1e2eae0, 0xc04d60c9c0})
	/go/pkg/mod/github.com/valyala/[email protected]/bytebuffer.go:45 +0x13e
io.copyBuffer({0x1e2e920, 0xc0299d9140}, {0x1e2eae0, 0xc04d60c9c0}, {0xc0293ca000, 0x1000, 0x1000})
	/usr/local/src/io/io.go:412 +0x14b
io.CopyBuffer({0x1e2e920?, 0xc0299d9140?}, {0x1e2eae0?, 0xc04d60c9c0?}, {0xc0293ca000?, 0xc0430019f0?, 0x2?})
	/usr/local/src/io/io.go:399 +0x3c
github.com/valyala/fasthttp.copyZeroAlloc({0x1e2e920, 0xc0299d9140}, {0x1e2eae0, 0xc04d60c9c0})
	/go/pkg/mod/github.com/valyala/[email protected]/http.go:1981 +0x7e
github.com/valyala/fasthttp.(*Request).bodyBytes(0xc03bb0d600)
	/go/pkg/mod/github.com/valyala/[email protected]/http.go:371 +0xec
github.com/valyala/fasthttp.(*Request).Body(0x20?)
	/go/pkg/mod/github.com/valyala/[email protected]/http.go:671 +0x9e
github.com/valyala/fasthttp.(*RequestCtx).PostBody(...)
	/go/pkg/mod/github.com/valyala/[email protected]/server.go:1415
github.com/valyala/fasthttp/fasthttpadaptor.ConvertRequest(0xc03bb0d600, 0xc04e9c6ea0, 0x1)
	/go/pkg/mod/github.com/valyala/[email protected]/fasthttpadaptor/request.go:15 +0x37
github.com/valyala/fasthttp/fasthttpadaptor.NewFastHTTPHandler.func1(0xc03bb0d600)
	/go/pkg/mod/github.com/valyala/[email protected]/fasthttpadaptor/adaptor.go:50 +0x85
github.com/valyala/fasthttp.TimeoutWithCodeHandler.func1.1()
	/go/pkg/mod/github.com/valyala/[email protected]/server.go:471 +0x33
created by github.com/valyala/fasthttp.TimeoutWithCodeHandler.func1
panic: runtime error: slice bounds out of range [:4167] with capacity 4096

goroutine 635068 [running]:
bufio.(*Reader).Read(0xc05aa93500, {0xc05b795ff7?, 0x9?, 0x4000?})
	/usr/local/src/bufio/bufio.go:249 +0x2ed
github.com/valyala/fasthttp.(*requestStream).Read(0xc04f278ff0, {0xc05b795ff7?, 0xc0669719d0?, 0xc05b77a000?})
	/go/pkg/mod/github.com/valyala/[email protected]/streaming.go:75 +0x2e5
github.com/valyala/bytebufferpool.(*ByteBuffer).ReadFrom(0xc041d8bdd0, {0x1e2eae0, 0xc04f278ff0})
	/go/pkg/mod/github.com/valyala/[email protected]/bytebuffer.go:45 +0x13e
io.copyBuffer({0x1e2e920, 0xc041d8bdd0}, {0x1e2eae0, 0xc04f278ff0}, {0xc01c746000, 0x1000, 0x1000})
	/usr/local/src/io/io.go:412 +0x14b
io.CopyBuffer({0x1e2e920?, 0xc041d8bdd0?}, {0x1e2eae0?, 0xc04f278ff0?}, {0xc01c746000?, 0x0?, 0xc0321a7b00?})
	/usr/local/src/io/io.go:399 +0x3c
github.com/valyala/fasthttp.copyZeroAlloc({0x1e2e920, 0xc041d8bdd0}, {0x1e2eae0, 0xc04f278ff0})
	/go/pkg/mod/github.com/valyala/[email protected]/http.go:1981 +0x7e
github.com/valyala/fasthttp.(*Request).bodyBytes(0xc033e59b80)
	/go/pkg/mod/github.com/valyala/[email protected]/http.go:371 +0xec
github.com/valyala/fasthttp.(*Request).Body(0x3?)
	/go/pkg/mod/github.com/valyala/[email protected]/http.go:671 +0x9e
github.com/valyala/fasthttp.(*RequestCtx).PostBody(...)
	/go/pkg/mod/github.com/valyala/[email protected]/server.go:1415
github.com/valyala/fasthttp/fasthttpadaptor.ConvertRequest(0xc033e59b80, 0xc066971ea0, 0x1)
	/go/pkg/mod/github.com/valyala/[email protected]/fasthttpadaptor/request.go:15 +0x37
github.com/valyala/fasthttp/fasthttpadaptor.NewFastHTTPHandler.func1(0xc033e59b80)
	/go/pkg/mod/github.com/valyala/[email protected]/fasthttpadaptor/adaptor.go:50 +0x85
github.com/valyala/fasthttp.TimeoutWithCodeHandler.func1.1()
	/go/pkg/mod/github.com/valyala/[email protected]/server.go:471 +0x33
created by github.com/valyala/fasthttp.TimeoutWithCodeHandler.func1
	/go/pkg/mod/github.com/valyala/[email protected]/server.go:470 +0x16b

source code

	handler := newCorsHandler(srv, cors)
	handler = newVHostHandler(vhosts, handler)

	fhandler := fasthttpadaptor.NewFastHTTPHandler(handler)
	fhandler = fasthttp.TimeoutHandler(fhandler, timeouts.ExecutionTimeout, "timeout")

	return &fasthttp.Server{
		Concurrency:        ConcurrencyLimit,
		Handler:            fhandler,
		ReadTimeout:        timeouts.ReadTimeout,
		WriteTimeout:       timeouts.WriteTimeout,
		IdleTimeout:        timeouts.IdleTimeout,
		MaxRequestBodySize: common.MaxRequestContentLength,
		StreamRequestBody: true,
		ReduceMemoryUsage:  true,
	}

func newCorsHandler(srv http.Handler, allowedOrigins []string) http.Handler {
	// disable CORS support if user has not specified a custom CORS configuration
	if len(allowedOrigins) == 0 {
		return srv
	}
	c := cors.New(cors.Options{
		AllowedOrigins: allowedOrigins,
		AllowedMethods: []string{http.MethodPost, http.MethodGet},
		MaxAge:         600,
		AllowedHeaders: []string{"*"},
	})
	return c.Handler(srv)
}

@li-jin-gou
Copy link
Contributor

Is it possible that you are using RequestCtx or any of their members after your handler has returned?

@erikdubbelboer
Copy link
Collaborator

I see you 👍 the comment above @JayChoi1736 , was that the issue? Can this be closed?

@JayChoi1736
Copy link
Author

i've checked, but doesn't work.
instead of that, turning off StreamRequestBody flag works. (1.34.0)

@gaby
Copy link
Contributor

gaby commented Feb 5, 2023

I have seen something similar happen if the request is sent without a Body and the server tries to read from it. But in the example code here it doesnt seem the request is being used.

@misiek08
Copy link

Can you show newVHostHandler?
I consider you check content-length, try to read whole request, but fasthttp is streaming it to you. Your handler is not streaming the request/response flow, so you get the error and panic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants