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: runtime error: invalid memory address using sync.WaitGroup #106

Closed
Rhymond opened this issue Apr 17, 2019 · 3 comments
Closed

panic: runtime error: invalid memory address using sync.WaitGroup #106

Rhymond opened this issue Apr 17, 2019 · 3 comments

Comments

@Rhymond
Copy link

Rhymond commented Apr 17, 2019

We get panic when we use xray with sync.WaitGroup. As you can see in a error below, context becomes nil and func getTraceHeaderFromContext throws panic as there is no required value in it.

we use algnhsa server adapter and mux.router for routing (it shouldn't cause an issue, but I added it to code example below) and we pass http.Request context to GetObjectWithContext function.

It looks like we're getting into some sort of race condition as it works 50% of time and another 50% we're getting a panic.

var s3c *s3.S3

func main() {
    s3c := s3.New(session.New())
    xray.AWS(s3c.Client)

    router := mux.NewRouter()
    router.Path("/").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        results := getObjects(r.Context(), []string{"1", "2", "3"})
        return
    })
    handler := xray.Handler(xray.NewFixedSegmentNamer("app"), router), err
    algnhsa.ListenAndServe(handler, nil)
}

func getOjbects(ctx context.Context, ids []string) []getResult {
    var wg sync.WaitGroup
    md := make([]getResult, len(ids))

    wg.Add(len(ids))
    for i, id := range ids {
        go func(key string, index int) {
            defer wg.Done()
            input := &s3.GetObjectInput{
                Bucket: aws.String(s.BucketName),
                Key:    aws.String(key),
            }
            result, err := s3c.GetObjectWithContext(ctx, input)
            md[index] = getResult{
                p:     result,
                err:   err,
            }
        }(id, i)
    }

    wg.Wait()
    // We're not reaching this point.
    return
}

Error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0xac7ff6]
goroutine 129 [running]:
github.com/aws/aws-xray-sdk-go/xray.getTraceHeaderFromContext(0x0, 0x0, 0x403e28)
/go/src/github.com/aws/aws-xray-sdk-go/xray/lambda.go:36 +0x26
github.com/aws/aws-xray-sdk-go/xray.BeginSubsegment(0x0, 0x0, 0xd39c1b, 0x3, 0x0, 0x0, 0x0)
/go/src/github.com/aws/aws-xray-sdk-go/xray/segment.go:167 +0x91
github.com/aws/aws-xray-sdk-go/xray.(*HTTPSubsegments).TLSHandshakeStart(0xc0001a8880)
/go/src/github.com/aws/aws-xray-sdk-go/xray/httptrace.go:101 +0x8d
github.com/aws/aws-xray-sdk-go/xray.NewClientTrace.func6()
/go/src/github.com/aws/aws-xray-sdk-go/xray/httptrace.go:226 +0x2a
net/http.(*persistConn).addTLS.func2(0xc0001a8900, 0xc0007a5c00, 0xc0008a4f00, 0xc000779620)
/usr/local/go/src/net/http/transport.go:1169 +0xa6
created by net/http.(*persistConn).addTLS
/usr/local/go/src/net/http/transport.go:1167 +0x17e
2019/04/17 09:39:42 unexpected EOF
2019/04/17 09:39:42 unexpected EOF
END RequestId: d9f5ac74-ea74-43ab-a1e2-89cfb5d29131
@t-yuki
Copy link
Contributor

t-yuki commented Apr 22, 2019

I've fixed it: #105

@luluzhao
Copy link
Contributor

@t-yuki Thank you so much for submitting that PR. I already merged it. @Rhymond I will make sure we included the fix for next release.

@luluzhao luluzhao reopened this Apr 30, 2019
@luluzhao luluzhao added this to the wait-to-be-released milestone Apr 30, 2019
@Rhymond
Copy link
Author

Rhymond commented May 1, 2019

Thanks both!

@Rhymond Rhymond closed this as completed May 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants