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

context: should panic at WithValue but not #54708

Closed
cuiweixie opened this issue Aug 27, 2022 · 7 comments
Closed

context: should panic at WithValue but not #54708

cuiweixie opened this issue Aug 27, 2022 · 7 comments
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@cuiweixie
Copy link
Contributor

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

$ go version
go version devel go1.20-18e01e9436 Sat Aug 27 15:34:47 2022 +0800 darwin/arm64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
darwin/arm64

What did you do?

package main

import (
	"context"
	"reflect"
)

func main() {
	ctx := context.Background()
	var key interface{} = [1]interface{}{[]int{1}}
	e := reflect.ValueOf(&key).Elem()
	key = e.Interface()
	ctx = context.WithValue(ctx, key, 1)
	val := ctx.Value(key)
	println(val)
}

What did you expect to see?

panic at line 13 when call WithValue

What did you see instead?

panic at line 14 when call ctx.Value(key)

@gopherbot
Copy link

Change https://go.dev/cl/426093 mentions this issue: context: should check comparable of value not type

@cuonglm cuonglm added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Aug 27, 2022
@cuonglm
Copy link
Member

cuonglm commented Aug 27, 2022

I'm not sure we should, this change will break user program that set context value with comparable key type, but non-comparable key value (like your example), but never call ctx.Value to get the value.

Also, the context.WithValue doc is also not so clear, it said:

The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using context.

Not specify key type or key value.

PS: next time, you should wait for discussing in the issue before sending a CL.

@cuiweixie
Copy link
Contributor Author

i think checking value is comparable is better

@gopherbot
Copy link

Change https://go.dev/cl/554457 mentions this issue: context: WithValue panic for uncomparable value

@Mgrdich
Copy link

Mgrdich commented Feb 26, 2024

i just want to understand the reason for it

why the inside the WithValue
we are checking it like this , instead of making this with generics ?

 if !reflectlite.TypeOf(key).Comparable() {
		panic("key is not comparable")
}

if we can make type value as comparable in the function params ?

func WithValue[T comparable](parent context.Context, key T, val any) context.Context {

@ianlancetaylor
Copy link
Contributor

The WithValue function was added to the API before generics were added to the language. Changing the signature of WithValue now would break the Go 1 compatibility guarantee.

@ianlancetaylor
Copy link
Contributor

I agree with @cuonglm : I don't think we should change this now. It's a very unusual case, and it's an easy problem to detect and to fix. I don't see a reason to risk breaking existing working code.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants