expvar: Func does not implement Var #42705
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?Go playground, at version go1.15.5.
Does this issue reproduce with the latest release?
Yes, this is present in go1.15.3, and in the playground at go1.15.5.
What operating system and processor architecture are you using (
go env
)?Go playground, but first seen on linux/amd64.
What did you do?
Used expvar.Publish with expvar.Func, with a function that returns a struct which encoding/json cannot serialize.
https://play.golang.org/p/9wpvZMJtDmw
What did you expect to see?
I expected expvar.Func to implement expvar.Var, meaning it would always return valid JSON. I expected that an HTTP request to /debug/vars would return a valid JSON object. I expected that expvar.Func would recover from an error by returning a boring but valid value, such as
null
.What did you see instead?
The String method of expvar.Func ignores the error return value from encoding/json.Marshal, so will return an empty string when the func returns a value that cannot be serialized to JSON.
https://github.com/golang/go/blob/go1.15.5/src/expvar/expvar.go#L261-L264
The text was updated successfully, but these errors were encountered: