Skip to content

Commit

Permalink
fix: memory leak fasthttp handler (#311)
Browse files Browse the repository at this point in the history
We can't use fasthttp.RequestCtx in NewSegmentHeader, this
generated an infinite wait in xray/segment.go#L135, because
fasthttp.RequestCtx.Done() is not called after finishing the request.

The solution was to create a new context to pass to NewSegmentHeader.
  • Loading branch information
nicolascb authored Jun 16, 2021
1 parent 7446e85 commit 25916b1
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion xray/fasthttp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xray

import (
"context"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -28,7 +29,9 @@ func NewFastHTTPInstrumentor(cfg *Config) FastHTTPHandler {
// Handler wraps the provided fasthttp.RequestHandler
func (h *fasthttpHandler) Handler(sn SegmentNamer, handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
auxCtx := context.Background()
if h.cfg != nil {
auxCtx = context.WithValue(context.Background(), fasthttpContextConfigKey, h.cfg)
ctx.SetUserValue(fasthttpContextConfigKey, h.cfg)
}

Expand All @@ -42,7 +45,7 @@ func (h *fasthttpHandler) Handler(sn SegmentNamer, handler fasthttp.RequestHandl
return
}

_, seg := NewSegmentFromHeader(ctx, name, req, traceHeader)
_, seg := NewSegmentFromHeader(auxCtx, name, req, traceHeader)
defer seg.Close(nil)

ctx.SetUserValue(fasthttpContextKey, seg)
Expand Down

0 comments on commit 25916b1

Please sign in to comment.