常见错误

包含了xstring基础库,内存占用多

文件信息:

image-20200805165006126

堆栈分析(参考:https://segmentfault.com/a/1190000019929993)

image-20200805165037014

解决方案

xstrings很多地方用,但pinyin用到的不多,而且内存占用比较多
像pinyin这种要单独一个包,不能放在通用包里面。

Proto返回的数组包含了nil,整个服务会异常退出

数组中带了nil的元素,会导致服务退出

image-20200813144253784

控制台错误堆栈

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

goroutine 1340 [running]:
gogs.xinyan.tech/beenest/proto/common.(*UserPermission).MarshalToSizedBuffer(0x0, 0xc000fe0a00, 0x2e8, 0x4ac, 0x93, 0x2e9, 0x0)
    /root/go/pkg/mod/gogs.xinyan.tech/beenest/proto@v0.0.0-20200812034637-b5800b40b350/common/base_channel.pb.go:10533 +0x26
gogs.xinyan.tech/beenest/proto/common.(*GetSubchannelPermissionRsp).MarshalToSizedBuffer(0xc0011dd050, 0xc000fe0a00, 0x4ac, 0x4ac, 0xbe3303, 0x1e73a20, 0x2050a40)
    /root/go/pkg/mod/gogs.xinyan.tech/beenest/proto@v0.0.0-20200812034637-b5800b40b350/common/base_channel.pb.go:14995 +0x38a
gogs.xinyan.tech/beenest/proto/common.(*GetSubchannelPermissionRsp).Marshal(0xc0011dd050, 0x2050a40, 0xc0011dd050, 0x7f6c1be198b8, 0xc0011dd050, 0xffffffffffffff01)
    /root/go/pkg/mod/gogs.xinyan.tech/beenest/proto@v0.0.0-20200812034637-b5800b40b350/common/base_channel.pb.go:14938 +0x7a
google.golang.org/protobuf/internal/impl.legacyMarshal(0x235e9a0, 0xc000dcd0d0, 0x0, 0x0, 0x0, 0x0, 0x7f6c1be19878, 0x2050a40, 0xc0011dd050, 0xc001a43070, ...)
    /root/go/pkg/mod/google.golang.org/protobuf@v1.23.0/internal/impl/legacy_message.go:391 +0xb0
google.golang.org/protobuf/proto.MarshalOptions.marshal(0xc000000001, 0x0, 0x0, 0x0, 0x235e9a0, 0xc000dcd0d0, 0xc000dcd0d0, 0x235e9a0, 0xc000dcd0d0, 0x2050a40, ...)
    /root/go/pkg/mod/google.golang.org/protobuf@v1.23.0/proto/encode.go:163 +0x2b9
google.golang.org/protobuf/proto.MarshalOptions.MarshalAppend(0x2000001, 0x0, 0x0, 0x0, 0x2300f80, 0xc000dcd0d0, 0x2050a40, 0x2336580, 0xc0011dd050, 0x2358420, ...)
    /root/go/pkg/mod/google.golang.org/protobuf@v1.23.0/proto/encode.go:125 +0x98
github.com/golang/protobuf/proto.marshalAppend(0x0, 0x0, 0x0, 0x2336580, 0xc0011dd050, 0x203000, 0x0, 0x0, 0x0, 0x0, ...)
    /root/go/pkg/mod/github.com/golang/protobuf@v1.4.2/proto/wire.go:40 +0xbe
github.com/golang/protobuf/proto.Marshal(...)
    /root/go/pkg/mod/github.com/golang/protobuf@v1.4.2/proto/wire.go:23
github.com/micro/go-micro/v2/server/grpc.protoCodec.Marshal(0x2050a40, 0xc0011dd050, 0x28, 0x1ee40e0, 0x0, 0x0, 0x28)
    /root/go/pkg/mod/github.com/micro/go-micro/v2@v2.9.1/server/grpc/codec.go:71 +0x66
github.com/micro/go-micro/v2/server/grpc.wrapCodec.Marshal(0x23404c0, 0x3304b00, 0x2050a40, 0xc0011dd050, 0xc000deb0e0, 0xc001a43430, 0x103dbd7, 0xc00004b748, 0xc0001c6d20)
    /root/go/pkg/mod/github.com/micro/go-micro/v2@v2.9.1/server/grpc/codec.go:51 +0x92
google.golang.org/grpc.encode(0x7f6bf0010170, 0xc0001c1300, 0x2050a40, 0xc0011dd050, 0x3c, 0xc001a43420, 0xc29a63, 0xc000b38656, 0xc001432990)
    /root/go/pkg/mod/google.golang.org/grpc@v1.26.0/rpc_util.go:543 +0x52
google.golang.org/grpc.prepareMsg(0x2050a40, 0xc0011dd050, 0x7f6bf0010170, 0xc0001c1300, 0x0, 0x0, 0x0, 0x0, 0xc00004b740, 0x1d8ea40, ...)
    /root/go/pkg/mod/google.golang.org/grpc@v1.26.0/stream.go:1519 +0x85
google.golang.org/grpc.(*serverStream).SendMsg(0xc0001c8d80, 0x2050a40, 0xc0011dd050, 0x0, 0x0)
    /root/go/pkg/mod/google.golang.org/grpc@v1.26.0/stream.go:1412 +0xfb
github.com/micro/go-micro/v2/server/grpc.(*grpcServer).processRequest(0xc000102a80, 0x234de00, 0xc0001c8d80, 0xc0005a4f00, 0xc0005a94d0, 0xc0016f5da0, 0x16, 0x2344900, 0xc00132ce00, 0x0, ...)
    /root/go/pkg/mod/github.com/micro/go-micro/v2@v2.9.1/server/grpc/grpc.go:425 +0xd88
github.com/micro/go-micro/v2/server/grpc.(*grpcServer).handler(0xc000102a80, 0x0, 0x0, 0x234de00, 0xc0001c8d80, 0x0, 0x0)
    /root/go/pkg/mod/github.com/micro/go-micro/v2@v2.9.1/server/grpc/grpc.go:315 +0x77f
google.golang.org/grpc.(*Server).processStreamingRPC(0xc000366d80, 0x23584e0, 0xc001654000, 0xc000e1c600, 0x0, 0xc0001a0cc0, 0x0, 0x0, 0x0)
    /root/go/pkg/mod/google.golang.org/grpc@v1.26.0/server.go:1237 +0xcd1
google.golang.org/grpc.(*Server).handleStream(0xc000366d80, 0x23584e0, 0xc001654000, 0xc000e1c600, 0x0)
    /root/go/pkg/mod/google.golang.org/grpc@v1.26.0/server.go:1323 +0xbb0
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc0011e4970, 0xc000366d80, 0x23584e0, 0xc001654000, 0xc000e1c600)
    /root/go/pkg/mod/google.golang.org/grpc@v1.26.0/server.go:722 +0xa1
created by google.golang.org/grpc.(*Server).serveStreams.func1
    /root/go/pkg/mod/google.golang.org/grpc@v1.26.0/server.go:720 +0xa1

源码分析

对于数组没有判空操作

image-20200813140747855

对于Map倒是有判空操作

image-20200813140945447

解决方案

生成的方法里面,虽然看起来返回了err,但有运行时空指针错误,所以需要增加recover

func (m *GetSubchannelPermissionRsp) Marshal() (dAtA []byte, err error) {

  // +++++++++++++++++++++++++++++
    defer func() {
        err2 := recover()
        if err2 != nil {
            println(err2)
            if err == nil {
                err, _ = err2.(error)
            }
        }
    }()
  // +++++++++++++++++++++++++++++

    size := m.Size()
    dAtA = make([]byte, size)
    n, err := m.MarshalToSizedBuffer(dAtA[:size])
    if err != nil {
        return nil, err
    }
    return dAtA[:n], nil
}

效果

selfcall日志正常捕捉,服务不会退出

len: 6
21:55.813 [Info] /common/wrapper/global/global.go:99 ctx.Deadline timeout: 14.999866176s
21:55.815 [exec: 1] [GRpc] Finish meet.dev.rpc.status : StatusService.GetSession, Rsp: {"session":{"token":"i7upozs887ddmyw4a11z7h99cw","userID":197,"lastActivityAt":1597292769,"expiredAt":1597897569,"device":2,"deviceID":"ac500ec33b1d70d44a1d114a77a3b81d","encrypter":"k4mpt4jqxjr8jn8z"}}
21:55.815 [Rpc] Start meet.dev.rpc.channel : ChannelService.GetSubchannelPermission
21:55.939 [exec: 123] [GRpc] Error meet.dev.rpc.channel : ChannelService.GetSubchannelPermission, error msg: {"id":"go.micro.client","code":500,"detail":"grpc: error while marshaling: runtime error: invalid memory address or nil pointer dereference","status":"Internal Server Error"}, Rsp: {}
21:55.939 [Error] /common/wrapper/global/global.go:139 Error: &errors.Error{
  Id:     go.micro.client,
  Code:   500,
  Detail: grpc: error while marshaling: runtime error: invalid memory address or nil pointer dereference,
  Status: Internal Server Error,
}
21:55.939 [Error] /common/wrapper/global/global.go:150 Stack: goroutine 52147 [running]:
runtime/debug.Stack(0xc001cc81e0, 0x1bb5f00, 0x2e9c370)
    /usr/local/go/src/runtime/debug/stack.go:24 +0x9d
gogs.xinyan.tech/beenest/common/wrapper/global.GetRecoverError(0x200b9c0, 0xc001cc81e0, 0x1d24b80, 0xc001b95560, 0xc000cc80f0, 0x1cbea80, 0x17)
    /root/go/pkg/mod/gogs.xinyan.tech/beenest/common@v0.0.0-20200811092222-e4242ab63a4a/wrapper/global/global.go:147 +0x493
gogs.xinyan.tech/beenest/common/wrapper/global.globalWrapper.func1.1.1(0xc000d59530, 0xc000d59560, 0x201b700, 0xc00030d680, 0xbfc55864f07d2c00, 0x123111a8d14d, 0x2e71200, 0x1d71960, 0xc001b08510)
    /root/go/pkg/mod/gogs.xinyan.tech/beenest/common@v0.0.0-20200811092222-e4242ab63a4a/wrapper/global/global.go:59 +0xc1
gogs.xinyan.tech/beenest/common/wrapper/global.globalWrapper.func1.1(0x200b9c0, 0xc001cc81e0, 0x201b700, 0xc00030d680, 0x1d71960, 0xc001b08510, 0x1fcdf20, 0xc001b95560)
    /root/go/pkg/mod/gogs.xinyan.tech/beenest/common@v0.0.0-20200811092222-e4242ab63a4a/wrapper/global/global.go:90 +0x2fb
github.com/micro/go-micro/v2/util/wrapper.AuthHandler.func1.1(0x200b9c0, 0xc001287f50, 0x201b700, 0xc00030d680, 0x1d71960, 0xc001b08510, 0xc001287f50, 0xc0002539d0)
    /root/go/pkg/mod/github.com/micro/go-micro/v2@v2.9.1/util/wrapper/wrapper.go:239 +0x649
github.com/micro/go-micro/v2/util/wrapper.TraceHandler.func1.1(0x200b9c0, 0xc001287e60, 0x201b700, 0xc00030d680, 0x1d71960, 0xc001b08510, 0x200b9c0, 0xc001287e60)
    /root/go/pkg/mod/github.com/micro/go-micro/v2@v2.9.1/util/wrapper/wrapper.go:120 +0x1c6
gogs.xinyan.tech/beenest/common/gateway.wrap.func1.1(0xc0001f5200, 0xc000913aa8, 0xc000913ad8, 0xc000d59ab8, 0xc000913a98, 0x2006c80, 0xc0014a16c0, 0x1e48348, 0xc0005d97a0, 0xc000913ac8, ...)
    /root/go/pkg/mod/gogs.xinyan.tech/beenest/common@v0.0.0-20200811092222-e4242ab63a4a/gateway/api.go:304 +0x318
gogs.xinyan.tech/beenest/common/gateway.wrap.func1(0x2006c80, 0xc0014a16c0, 0xc0001f5200)
    /root/go/pkg/mod/gogs.xinyan.tech/beenest/common@v0.0.0-20200811092222-e4242ab63a4a/gateway/api.go:343 +0x9ea
....
Copyright © xinyan all right reserved,powered by Gitbook该文件修订时间: 2020-08-13 15:32:10

results matching ""

    No results matching ""