运行时错误:无效的内存地址或无指针取消引用,grpc golang示例

时间:2016-09-29 13:24:33

标签: go grpc

我正在尝试使用仅用于服务器端流的方法来实现简单的grpc服务器和客户端模型。我正在追踪route guide example in offical repo。尝试运行时,我从服务器端收到此错误

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x4011fc]

goroutine 3 [running]:
panic(0x878c80, 0xc82000a150)
    /usr/local/go/src/runtime/panic.go:481 +0x3e6
main.(*server).Podips(0xc820136188, 0xc82000aa40, 0x7f1179b9f530, 0xc82000aa90, 0x0, 0x0)
    /home/tcs/gowork/src/poc/test/server.go:22 +0x1fc
poc/test/pb._Getips_Podips_Handler(0x856f00, 0xc820136188, 0x7f1179b9f438, 0xc8201da000, 0x0, 0x0)
    /home/tcs/gowork/src/poc/test/pb/getip.pb.go:129 +0x175
google.golang.org/grpc.(*Server).processStreamingRPC(0xc82019a640, 0x7f1179b245a0, 0xc8201ca000, 0xc8201d0000, 0xc820169440, 0xb7afa0, 0xc82016a570, 0x0, 0x0)
    /home/tcs/gowork/src/google.golang.org/grpc/server.go:689 +0x489
google.golang.org/grpc.(*Server).handleStream(0xc82019a640, 0x7f1179b245a0, 0xc8201ca000, 0xc8201d0000, 0xc82016a570)
    /home/tcs/gowork/src/google.golang.org/grpc/server.go:773 +0x1151
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc820133020, 0xc82019a640, 0x7f1179b245a0, 0xc8201ca000, 0xc8201d0000)
    /home/tcs/gowork/src/google.golang.org/grpc/server.go:422 +0xa0
created by google.golang.org/grpc.(*Server).serveStreams.func1
    /home/tcs/gowork/src/google.golang.org/grpc/server.go:423 +0x9a
exit status 2

这是来自客户端

2016/09/29 22:31:37 transport: http2Client.notifyError got notified that the client transport was broken EOF.
2016/09/29 22:31:37 &{0xc820192b40}.Podips(_) = _, rpc error: code = 13 desc = transport is closing
exit status 1

server.go

package main

import (
    "net"
    "flag"
    "fmt"

    "google.golang.org/grpc/grpclog"
    "google.golang.org/grpc"
    pb "poc/test/pb"
)

var port = flag.Int("port", 10000, "The server port")
type server struct{
  ip *pb.Ips
}

func (s *server) Podips(n *pb.Request, stream pb.Getips_PodipsServer)   (error){

    res := [3]string{"firstIP", "secondIp", "thirdIP"}

    for _, v := range res {

        s.ip.Ip = v
        if s.ip == nil{
            if err := stream.Send(s.ip); err != nil {
                    return err
            }
        }
    }

  return  nil
}

func main(){
    flag.Parse()
    lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
    if err != nil {
        grpclog.Fatalf("failed to listen: %v", err)
    }
    grpcServer := grpc.NewServer()
    grpclog.Println("server descrption:%v", grpcServer) 
    pb.RegisterGetipsServer(grpcServer, new(server))
    grpcServer.Serve(lis)

}

some.proto文件

syntax = "proto3";

package getips;

service Getips {
    rpc Podips (Request) returns (stream Ips){}

}

message Request {
    string req = 1;

}
message Ips {
    string ip = 1;
}

我正在通过关于nil指针错误的堆栈中的几个问题我发现这个代码是罪魁祸首

s.ip.Ip = v
if s.ip == nil{
    if err := stream.Send(s.ip); err != nil {
          return err
    }

我不知道如何解决这个问题,我怀疑我是以错误的方式实施它。 任何帮助都会很棒。 感谢

1 个答案:

答案 0 :(得分:0)

您正在执行new(server)而未初始化字段server.ip。我不知道实现的细节,但您基本上需要先初始化此字段,否则s.ipnil,当您调用s.ip.Ip时会出现错误。