Search code examples
goprotocol-buffers

invalid memory address when creating grpc client request with repeated field


I have an issue with my grpc server and client, I'm trying to create a repeated field in the grpc and send it to the client. but it failed on error, what am I doing wrong on my grpc client/server? i tried send the RequestsCommand as value but this didn't work to.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x165cf0e]

goroutine 40 [running]:
main.(*Server).RunCommands(0x172b960?, {0x18be540?, 0xc00031c030?}, 0x107a360?)
        <autogenerated>:1 +0x2e
github.com/mygithub/some-package/proto._ExecuterService_RunCommands_Handler({0x1701a20?, 0xc0002430d0}, {0x18be540, 0xc00031c030}, 0xc000318070, 0x0)
        ..../executer_grpc.pb.go:93 +0x170

executer_grpc.pb.go:93 is :

in := new(CommandsRequest)
return srv.(ExecuterServiceServer).RunCommands(ctx, in)

on the server side I have the following:

grpc_server.go:

package main

import (
    "fmt"
    pb "github.com/mygithub/some-package/proto"
    "google.golang.org/grpc"
)


type Server struct {
    pb.ExecuterServiceServer
}

func startServer() {
    addr := GetAgentAddress()
    lis, err := net.Listen("tcp", addr)

    if err != nil {
        log.Fatalf("Faild to listen on %v\n", err)
    }
    s := grpc.NewServer()
    pb.RegisterExecuterServiceServer(s, &Server{})

    if err = s.Serve(lis); err != nil {
        log.Fatalf("Failed to serve: %v\n", err)
    }

}

grpc_client.go:

package main

import (
    "context"
    "encoding/json"
    "fmt"
    v1 "github.com/mygithub/some-package/api/v1"
    pb "github.com/mygithub/some-package/proto"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
)

func sendCommandsWithClient() (string, error) {
    command := v1.CommandSpec{
        BashCommand: "ls",
        Async:       false,
    }

    addr := "localhost:4000"
    conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        return "", err
    }

    defer conn.Close()
    client := pb.NewExecuterServiceClient(conn)
    if err != nil {
        return "", err
    }

    commandRequest := &pb.CommandRequest{
        BashCommand: command.BashCommand,
        Slug:        "test",
        Async:       command.Async,
    }

    req := &pb.CommandsRequest{}
    req.Commands = append(req.Commands, commandRequest)
    aAsJsonString, _ := json.Marshal(req)
    fmt.Println(string(aAsJsonString))
    res, err := client.RunCommands(context.Background(), req)

    if err != nil {
        return "", err
    }
    return res.Content, nil
}

func main() {
    _, err := sendCommandsWithClient()
    if err != nil {
        return
    }
}

RunCommands:

func (s *Server) RunCommands(ctx context.Context, in *pb.CommandsRequest) (*pb.CommandsResponse, error) {
    log.Println("test")
    command := v1.CommandSpec{BashCommand: "ls", Slug: "test", Async: true}
    CommandsQueue := append(CommandsQueue, command)
    size := len(CommandsQueue)
    return &pb.CommandsResponse{Status: 0, Content: strconv.Itoa(size)}, nil
}

go version: go version go1.18.2 darwin/amd64 protoc: proto3


Solution

  • you should go build or go run with all of your go files.

    go run main.go model.go somefile.go