syb
syb

Reputation: 62

Golang gRPC Client Panics with "invalid memory address" when Connecting to Unix Socket

Tracee supports three different services. My CLI tool currently supports two.

type Server struct {
    Addr             string
    conn             *grpc.ClientConn
    diagnosticClient pb.DiagnosticServiceClient
    serviceClient    pb.TraceeServiceClient
}

Here's the function to connect to Tracee:

func (s Server) Connect() error {
    var opts []grpc.DialOption
    opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
    conn, err := grpc.Dial("unix://"+s.Addr, opts...) // Corrected to grpc.Dial
    if err != nil {
        return err
    }
    s.conn = conn
    s.diagnosticClient = pb.NewDiagnosticServiceClient(s.conn)
    s.serviceClient = pb.NewTraceeServiceClient(s.conn)
    return nil
}

I'm testing it with the version command, which is a simple one. Here's the code for it:

func (tc *Server) GetVersion(ctx context.Context, req *pb.GetVersionRequest) (*pb.GetVersionResponse, error) {
    return tc.serviceClient.GetVersion(ctx, req)
}

It receives the context and request for the version, but it fails with the following error:

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

goroutine 1 [running]:
github.com/aquasecurity/tracee/cmd/traceectl/pkg/client.(*Server).GetVersion(...)
        /home/shoham/shoham/tracee/cmd/traceectl/pkg/client/service.go:10
github.com/aquasecurity/tracee/cmd/traceectl/pkg/cmd.Version.Run({0xc000131000?, 0xf092a0?}, {0xb22b70, 0xf35a20})
        /home/shoham/shoham/tracee/cmd/traceectl/pkg/cmd/version.go:19 +0x4c
// ... rest of the stack trace

I've spent a lot of time trying to resolve this, but I haven't been able to figure out why I'm getting this "invalid memory address" error. Any help would be greatly appreciated!

Here's what I've tried:

I suspect there might be a problem with the Unix socket itself, but I can't figure out what it is. Any help would be greatly appreciated!

Upvotes: 1

Views: 42

Answers (1)

Tyler Kropp
Tyler Kropp

Reputation: 573

Your code to connect does not correctly set the values in the struct, because you are using a value receiver on your Connect method. Change it to use a pointer receiver so that the values inside the struct get set:

// before
func (s Server) Connect() error

// after
func (s *Server) Connect() error

See also this question: Value receiver vs. pointer receiver

Upvotes: 0

Related Questions