-
Notifications
You must be signed in to change notification settings - Fork 18k
net/rpc: zero values not transmitted using encoding/gob #8997
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
Comments
I rebuild and run server code in -race flag, and edit rpc server code as below: ---------------------------------------- //rpc server package main import ( "fmt" "net" "net/rpc" "os" ) type Args struct { A int } type Rsp struct { A int } type Echoer int var isFirst = true func (t *Echoer) Echo(args *Args, reply *Rsp) error { if isFirst { reply.A = args.A isFirst = false } else { reply.A = 0 } fmt.Println("reply-----", reply.A) return nil } func main() { echoer := new(Echoer) rpc.Register(echoer) tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234") checkError(err) listener, err := net.ListenTCP("tcp", tcpAddr) checkError(err) conn, err := listener.Accept() checkError(err) rpc.ServeConn(conn) } func checkError(err error) { if err != nil { fmt.Println("Fatal error ", err.Error()) os.Exit(1) } } ---------------------------------- The output of both client and server have nothing different from original. And sending Ctrl-C to client, server exit with no warning or error. The Processor of this server is only one, I don't think this problem is related to concurrent or race condition. Maybe some strange behavior of gob codec (zero value) is the answer. |
Running to the same issue here. When reply value is 0(for int type), it seems that the decoder just overlook this field. A simpler way to reproduce is, |
Still have the same issue with Go 1.7.1. When will this be fixed? |
This is because net/rpc use encoding/gob as default codec. And if a field has the zero value for its type, it is omitted from the transmission. So you should start with a clean, zeroed item before decoding. func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: ", os.Args[0], "server:port")
os.Exit(1)
}
service := os.Args[1]
client, err := rpc.Dial("tcp", service)
if err != nil {
log.Fatal("dialing:", err)
}
args := Args{3}
// var rsp Rsp
for {
var rsp Rsp // make it clean
err = client.Call("Echoer.Echo", args, &rsp)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Echoer: A=%d\n", rsp.A)
time.Sleep(time.Second * 2)
}
} |
Just got bit by this bug. I wanted to use a pointer in a struct to differentiate between the value existing vs. the value being the zero value, but gob doesn't support this use case correctly. Reading through the spec of gob encoding, this shouldn't be the intended behaviour.
The zero value for |
I recently discovered that the JSON encoding https://pkg.go.dev/encoding/json#Marshal
A non-nil pointer to a boolean that is |
I believe this is working as expected for encoding/gob. |
by HongF.Yue:
The text was updated successfully, but these errors were encountered: