Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server/embed: fix data race when start insecure grpc #15509

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions server/embed/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,29 @@ func (sctx *serveCtx) serve(
servElection := v3election.NewElectionServer(v3c)
servLock := v3lock.NewLockServer(v3c)

var gs *grpc.Server
defer func() {
if err != nil && gs != nil {
sctx.lg.Warn("stopping grpc server due to error", zap.Error(err))
gs.Stop()
sctx.lg.Warn("stopped grpc server due to error", zap.Error(err))
}
}()

// Make sure serversC is closed even if we prematurely exit the function.
defer close(sctx.serversC)

if sctx.insecure {
gs = v3rpc.Server(s, nil, nil, gopts...)
gs := v3rpc.Server(s, nil, nil, gopts...)
v3electionpb.RegisterElectionServer(gs, servElection)
v3lockpb.RegisterLockServer(gs, servLock)
if sctx.serviceRegister != nil {
sctx.serviceRegister(gs)
}

defer func(gs *grpc.Server) {
if err != nil {
sctx.lg.Warn("stopping insecure grpc server due to error", zap.Error(err))
gs.Stop()
sctx.lg.Warn("stopped insecure grpc server due to error", zap.Error(err))
}
}(gs)

grpcl := m.Match(cmux.HTTP2())
go func() { errHandler(gs.Serve(grpcl)) }()
go func(gs *grpc.Server, grpcLis net.Listener) {
errHandler(gs.Serve(grpcLis))
}(gs, grpcl)

var gwmux *gw.ServeMux
if s.Cfg.EnableGRPCGateway {
Expand All @@ -156,7 +158,10 @@ func (sctx *serveCtx) serve(
return err
}
httpl := m.Match(cmux.HTTP1())
go func() { errHandler(srvhttp.Serve(httpl)) }()

go func(srvhttp *http.Server, httpLis net.Listener) {
errHandler(srvhttp.Serve(httpLis))
}(srvhttp, httpl)

sctx.serversC <- &servers{grpc: gs, http: srvhttp}
sctx.lg.Info(
Expand All @@ -170,12 +175,22 @@ func (sctx *serveCtx) serve(
if tlsErr != nil {
return tlsErr
}
gs = v3rpc.Server(s, tlscfg, nil, gopts...)

gs := v3rpc.Server(s, tlscfg, nil, gopts...)
v3electionpb.RegisterElectionServer(gs, servElection)
v3lockpb.RegisterLockServer(gs, servLock)
if sctx.serviceRegister != nil {
sctx.serviceRegister(gs)
}

defer func(gs *grpc.Server) {
if err != nil {
sctx.lg.Warn("stopping secure grpc server due to error", zap.Error(err))
gs.Stop()
sctx.lg.Warn("stopped secure grpc server due to error", zap.Error(err))
}
}(gs)
Comment on lines +186 to +192
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment: the code block is almost identical to the code block for insecure. I am thinking to define a separate function something like below, then call it in both secure and insecure code block.

func closegRPCServer(gs *grpc.Server) {
  ...
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it needs return error to the log, I'm not sure how to handle this with your idea. In here, I follow the defer pattern~


handler = grpcHandlerFunc(gs, handler)

var gwmux *gw.ServeMux
Expand Down Expand Up @@ -208,7 +223,10 @@ func (sctx *serveCtx) serve(
sctx.lg.Error("Configure https server failed", zap.Error(err))
return err
}
go func() { errHandler(srv.Serve(tlsl)) }()

go func(srvhttp *http.Server, tlsLis net.Listener) {
errHandler(srvhttp.Serve(tlsLis))
}(srv, tlsl)

sctx.serversC <- &servers{secure: true, grpc: gs, http: srv}
sctx.lg.Info(
Expand Down
3 changes: 3 additions & 0 deletions tests/e2e/cmux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func fetchGrpcGateway(endpoint string, httpVersion string, connType e2e.ClientCo
}
req := e2e.CURLReq{Endpoint: "/v3/kv/range", Value: string(rangeData), Timeout: 5, HttpVersion: httpVersion}
respData, err := curl(endpoint, "POST", req, connType)
if err != nil {
return err
}
return validateGrpcgatewayRangeReponse([]byte(respData))
}

Expand Down