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

valueCtx call Value can cause panic #41254

Closed
Wang-Jun-Chao opened this issue Sep 7, 2020 · 6 comments
Closed

valueCtx call Value can cause panic #41254

Wang-Jun-Chao opened this issue Sep 7, 2020 · 6 comments

Comments

@Wang-Jun-Chao
Copy link

in context/context.go, I find some sence can lead valueCtx.Value panic

// create valueCtx, pass nil parent Context
func WithValue(parent Context, key, val interface{}) Context {
	if key == nil {
		panic("nil key")
	}
	if !reflectlite.TypeOf(key).Comparable() {
		panic("key is not comparable")
	}
	return &valueCtx{parent, key, val}
}

// get value
func (c *valueCtx) Value(key interface{}) interface{} {
	if c.key == key {
		return c.val
	}
	return c.Context.Value(key) //  if c.Context is nil, then call Value will panic !!!
}
@davecheney
Copy link
Contributor

Thank you for raising this issue. Would you please supply a runnable code sample that shows the problem. Thank you

@Wang-Jun-Chao
Copy link
Author

Wang-Jun-Chao commented Sep 7, 2020

@davecheney
case code

package main

import (
	"context"
	"fmt"
)

func main() {
	ctx := context.WithValue(nil, "test-key", "test-val")
	val := ctx.Value("not-exist-key")

	fmt.Println(val)
}

result

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

goroutine 1 [running]:
context.(*valueCtx).Value(0xc00006a180, 0x10ac320, 0x10e9f50, 0x10e9f30, 0x10ac320)
        /usr/local/go/src/context/context.go:553 +0x3f
main.main()
        /Users/wangjunchao/go/src/wangjunchao/value_context_panic/value_context_panic.go:10 +0x82

@davecheney
Copy link
Contributor

Please don’t pass nil as the parent context. This is documented in the package preamble.

Do not pass a nil Context, even if a function permits it. Pass context.TODO if you are unsure about which Context to use.

https://godoc.org/context

@mvdan
Copy link
Member

mvdan commented Sep 7, 2020

The Go project doesn't use the issue tracker for questions. See https://golang.org/wiki/Questions.

It seems like Dave clarified your problem above, so I'm closing this for now. In the future, please try one of the forums for questions.

@mvdan mvdan closed this as completed Sep 7, 2020
@royprotim
Copy link

royprotim commented May 4, 2021

Is the line

return c.Context.Value(key)

Concurrent Safe?

The stack trace of error I am receiving is attached
Screenshot 2021-05-04 at 3 57 19 PM

This panic is arising when a lot of read is happening to context value.

@mvdan
Copy link
Member

mvdan commented May 4, 2021

Please don't comment on old issues that were closed as questions. See the wiki link I shared.

@golang golang locked as resolved and limited conversation to collaborators May 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants