Skip to content

ValueContext ctx.Value() panic #58846

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

Closed
Khachi-at opened this issue Mar 3, 2023 · 1 comment
Closed

ValueContext ctx.Value() panic #58846

Khachi-at opened this issue Mar 3, 2023 · 1 comment

Comments

@Khachi-at
Copy link

Khachi-at commented Mar 3, 2023

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

$ go version
go1.16

Does this issue reproduce with the latest release?

Don't now

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

context

Code example as follow.But I can't reappear agin. What can i do for it?

package test

import "context"

type Test struct {
	TraceId string
	context context.Context
}

func (p *Test) Context() context.Context {
	if p.context == nil {
		//p.context = context.WithValue(context.Background(), "trace_id", p.TraceId)
		//start := time.Now()
		//p.context = context.WithValue(p.context, "start_time", &start)
		traceId := "dddddddddddddsdd"
		p.context = context.WithValue(context.Background(), "trace_id", traceId)
	}
	return p.context
}
package main

import test "gitlab.ctyun.cn/vnet/ops/smoke-online/script/test_pkg"

var ch = make(chan *test.Test)

func getCh() chan *test.Test {
	return ch
}

func main() {
	go func() {
		testIns := &test.Test{
			TraceId: "xxxxx",
		}

		getCh() <- testIns
	}()

	newTestIns := <-ch
	trace_id, _ := newTestIns.Context().Value("trace_id").(string)
	println(trace_id)
}

Panic

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

goroutine 293 [running]:
panic({0xcc9360, 0x14976e0})
	/usr/local/go/src/runtime/panic.go:987 +0x3ba fp=0xc0003f14f0 sp=0xc0003f1430 pc=0x4379ba
runtime.panicmem(...)
	/usr/local/go/src/runtime/panic.go:260
runtime.sigpanic()
	/usr/local/go/src/runtime/signal_unix.go:835 +0x2f6 fp=0xc0003f1540 sp=0xc0003f14f0 pc=0x44e636
context.(*valueCtx).Value(0xc000414000?, {0xc86b80?, 0xf21920?})
	/usr/local/go/src/context/context.go:563 +0x22 fp=0xc0003f1578 sp=0xc0003f1540 pc=0x498782
gitlab.xxxx.cn/vnet/xxx-utils/log.(*Logger).OutputContext(0xc000229f20, {0xf2b8d8, 0x0}, 0x0?, 0x2, {0xdfe97b, 0x31}, {0xc0003f1960, 0x1, 0x1})
@Khachi-at Khachi-at changed the title affected/package: ValueContext ctx.Value() panic Mar 3, 2023
@seankhliao
Copy link
Member

1.16 is outside of our supported range

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

No branches or pull requests

3 participants