17Twenty
17Twenty

Reputation: 297

Go bug when marshalling data using JSON-RPC - am I being dumb?

So I'm working with code similar to the following and can't get the RPC to work in the FooHolder where it holds objects that meets an interface. I might be missing something but it looks like it should work - using the reflect package it correctly identifies the types but there's something wrong when the function returns....

package main

import (
    "log"
    "net"
    "net/rpc"
    "net/rpc/jsonrpc"
    "reflect"
)

type FooInterface interface {
    DoTheBartman(in *string, out *string) error
    DoThis(in *[]string, out *[]string) error
    NoProblems(in *int, out *int) error
}

type Foo struct {
    wibble string
}

func (f *Foo) DoTheBartman(in *string, out *string) error {
    log.Println("do the bartman")
    *out = "^^[ " + *in + "]^^"
    return nil
}

func (f *Foo) DoThis(in *[]string, out *[]string) error {
    log.Println("Doing this")
    log.Println("Type in - ", reflect.TypeOf(in))
    log.Println("Type *in - ", reflect.TypeOf(*in))
    log.Println("Type out - ", reflect.TypeOf(out))
    log.Println("Type *out - ", reflect.TypeOf(*out))

    *out = append(*in, "Hello")

    return nil
}

type FooHolder struct {
    TheFoos []FooInterface
}

func (fh *FooHolder) GetFoos(in *[]Foo, out *[]Foo) error {
    log.Println("Getting Foos...")
    log.Println("Type in - ", reflect.TypeOf(in))
    log.Println("Type *in - ", reflect.TypeOf(*in))
    log.Println("Type out - ", reflect.TypeOf(out))
    log.Println("Type *out - ", reflect.TypeOf(*out))

    return nil // This fails but with a nil... what's up with that.
}

^^ This is the function that fails, not sure why? It returns a nil (and it's reported as a nil but errors out with a exit 1

func (f *Foo) NoProblems(in *int, out *int) error {
    log.Println("No Problems doing maths")
    log.Println("Type in - ", reflect.TypeOf(in))
    log.Println("Type *in - ", reflect.TypeOf(*in))
    log.Println("Type out - ", reflect.TypeOf(out))
    log.Println("Type *out - ", reflect.TypeOf(*out))

    *out = 42 - (*in)
    return nil
}

// This all works just fine
func startServer(f FooInterface) {
    server := rpc.NewServer()
    server.Register(f)

    l, e := net.Listen("tcp", ":8222")
    if e != nil {
        log.Fatal("listen error:", e)
    }

    for {
        conn, err := l.Accept()
        if err != nil {
            log.Fatal(err)
        }
        log.Println("Incoming!")

        go server.ServeCodec(jsonrpc.NewServerCodec(conn))
    }
}

// It starts to go wrong here...
func startFooHolderServer(f *FooHolder) {
    server := rpc.NewServer()
    server.Register(f)

    l, e := net.Listen("tcp", ":8222")
    if e != nil {
        log.Fatal("listen error:", e)
    }

    for {
        conn, err := l.Accept()
        if err != nil {
            log.Fatal(err)
        }
        log.Println("Incoming!")

        go server.ServeCodec(jsonrpc.NewServerCodec(conn))
    }
}

func main() {

    foo1 := &Foo{}

    fooHolder := &FooHolder{}

    fooHolder.TheFoos = append(fooHolder.TheFoos, foo1)

    // go startServer(foo1)
    go startFooHolderServer(fooHolder)

    conn, err := net.Dial("tcp", "localhost:8222")

    if err != nil {
        panic(err)
    }
    defer conn.Close()

    c := jsonrpc.NewClient(conn)

    var foo []Foo
    log.Println("Type - ", reflect.TypeOf(foo))
    log.Println("Type - ", reflect.TypeOf(&foo))
    err = c.Call("FooHolder.GetFoos", foo, &foo)
    if err != nil {
        log.Fatal("RPC error:", err)
    }

    // No Problems?
    var baz int
    err = c.Call("Foo.NoProblems", baz, &baz)
    if err != nil {
        log.Fatal("RPC error:", err)
    }
    log.Println("Yay - baz is now", baz)

    stringArg := "Put stuff around me"
    err = c.Call("Foo.DoTheBartman", stringArg, &stringArg)
    if err != nil {
        log.Fatal("RPC error:", err)
    }
    log.Println("Yay - stringArg is now", stringArg)

    // Also No Problem
    bar := append([]string{"Hello"}, stringArg)
    log.Println("Type - ", reflect.TypeOf(bar))
    err = c.Call("Foo.DoThis", bar, &bar)
    if err != nil {
        log.Fatal("RPC error:", err)
    }
    log.Println("Yay - bar is now", bar)
}

Output:

rpc-demo $ go run demo.go 
2015/09/05 20:48:32 Incoming!
2015/09/05 20:48:32 Type -  []main.Foo
2015/09/05 20:48:32 Type -  *[]main.Foo
2015/09/05 20:48:32 Getting Foos...
2015/09/05 20:48:32 Type in -  *[]main.Foo
2015/09/05 20:48:32 Type *in -  []main.Foo
2015/09/05 20:48:32 Type out -  *[]main.Foo
2015/09/05 20:48:32 Type *out -  []main.Foo
2015/09/05 20:48:32 RPC error:invalid error <nil>
exit status 1
rpc-demo $ 

Has anyone experienced similar - it doesn't help if I declare a new type and throw that around either and I'm not seeing a way forward.

Tangentially, but possibly related, I compiled a Go app to archive and built it into a C program before changing the interface to match a Foo rather than a Foo interface and that worked! Weird.

Upvotes: 0

Views: 394

Answers (1)

HectorJ
HectorJ

Reputation: 6324

Here is the line throwing that error : https://github.com/golang/go/blob/master/src/net/rpc/jsonrpc/client.go#L89

It is client-side, and you can see it is reached if c.resp.Error != nil || c.resp.Result == nil.

Now you don't return any error, so the problem is not here.

I believe the problem is that you do not set any result. GetFoos leaves the out pointer nil. Set out to a non-nil value and it should work.

Upvotes: 1

Related Questions