dongwu9170 2017-08-11 17:43
浏览 413

运行并发请求时,转到gRPC status.Error()导致无效的内存地址

I have a bcrypt.CompareHashAndPassword() function that if the error is not nil return a status.Error() back to the client.

It works If I don't run concurrent requests.

If I just return the err back to the client instead of status.Error() from the bcrypt everything works with concurrent requests. I ran it testing race conditions and nothing came up.

I'm not sure if the issue is with the "google.golang.org/grpc/status" library or with "golang.org/x/crypto/bcrypt". I looked into the status library and there are no reading/writing of maps or anything in my mind that would cause this issue. Any help appreciated.

My code:

  package main

import (
    pb "dms-cloud/authentication-microservice/pb"
    _ "github.com/go-sql-driver/mysql"
    "golang.org/x/net/context"
    "golang.org/x/crypto/bcrypt"
    "github.com/satori/go.uuid"
    "time"
    "encoding/json"
    "google.golang.org/grpc/status"
)

func (s *server) Authenticate(ctx context.Context, in *pb.AuthenticationRequest) (*pb.Ticket, error) {

    // Verify Password Correct
    err := bcrypt.CompareHashAndPassword([]byte(in.EncryptedPassword), []byte(in.Password))
    if err != nil {
        return nil, status.Error(105, "Authentication Failed")
    }

    // Generate Ticket
    tkt := uuid.NewV4()
    exp := time.Now().Unix() + 700

    // Response
    t := pb.Ticket{
        Uuid: tkt.String(),
        TimeExpire: exp,
        AccountId: in.AccountId,
    }

    rTkt, err := json.Marshal(t)

    err = cache.Set(tkt.String(), string(rTkt), time.Minute*60).Err()
    if err != nil {
        return nil, status.Error(500, "Internal Error")
    }

    return &t, nil
}

The error:

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

goroutine 34 [running]:
main.(*server).Authenticate(0x176f580, 0x1b44000, 0xc420194210, 0xc420192e00, 0x176f580, 0x102906e, 0x0)
        /Users/kenfab/go/src/dms-cloud/authentication-microservice/Authenticate.go:19 +0x105
dms-cloud/authentication-microservice/pb._AuthenticationMicroservice_Authenticate_Handler(0x1453060, 0x176f580, 0x1b44000, 0xc420194210, 0xc420016cd0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /Users/kenfab/go/src/dms-cloud/authentication-microservice/pb/authenticate.pb.go:240 +0x28d
google.golang.org/grpc.(*Server).processUnaryRPC(0xc4200128c0, 0x1717ec0, 0xc42001b1e0, 0xc42019c000, 0xc420019e60, 0x17464c0, 0xc4201625a0, 0x0, 0x0)
        /Users/kenfab/go/src/google.golang.org/grpc/server.go:781 +0xc41
google.golang.org/grpc.(*Server).handleStream(0xc4200128c0, 0x1717ec0, 0xc42001b1e0, 0xc42019c000, 0xc4201625a0)
        /Users/kenfab/go/src/google.golang.org/grpc/server.go:981 +0x15a6
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc420011b50, 0xc4200128c0, 0x1717ec0, 0xc42001b1e0, 0xc42019c000)
        /Users/kenfab/go/src/google.golang.org/grpc/server.go:551 +0xa9
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /Users/kenfab/go/src/google.golang.org/grpc/server.go:552 +0xa1
exit status 2
  • 写回答

1条回答 默认 最新

  • dongpin4611 2017-08-11 17:57
    关注

    Added a code from godocs does solve this issue. It only works with 0 - 15. Thank You @jeevatkm

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办