Skip to content

Commit 2afdd4a

Browse files
committed
Avoid deadlock in etcd.Close when stopping during bootstrapping
Signed-off-by: Joshua Zhang <[email protected]>
1 parent 9b24194 commit 2afdd4a

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

server/embed/etcd.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type Etcd struct {
8686
errc chan error
8787

8888
closeOnce sync.Once
89+
wg sync.WaitGroup
8990
}
9091

9192
type peerListener struct {
@@ -111,7 +112,7 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
111112
if !serving {
112113
// errored before starting gRPC server for serveCtx.serversC
113114
for _, sctx := range e.sctxs {
114-
close(sctx.serversC)
115+
sctx.close()
115116
}
116117
}
117118
e.Close()
@@ -436,6 +437,7 @@ func (e *Etcd) Close() {
436437
}
437438
}
438439
if e.errc != nil {
440+
e.wg.Wait()
439441
close(e.errc)
440442
}
441443
}
@@ -880,6 +882,9 @@ func (e *Etcd) serveMetrics() (err error) {
880882
}
881883

882884
func (e *Etcd) errHandler(err error) {
885+
e.wg.Add(1)
886+
defer e.wg.Done()
887+
883888
select {
884889
case <-e.stopc:
885890
return

server/embed/serve.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
"net/http"
2525
"strings"
26+
"sync"
2627

2728
etcdservergw "go.etcd.io/etcd/api/v3/etcdserverpb/gw"
2829
"go.etcd.io/etcd/client/pkg/v3/transport"
@@ -65,6 +66,7 @@ type serveCtx struct {
6566
userHandlers map[string]http.Handler
6667
serviceRegister func(*grpc.Server)
6768
serversC chan *servers
69+
closeOnce sync.Once
6870
}
6971

7072
type servers struct {
@@ -100,6 +102,9 @@ func (sctx *serveCtx) serve(
100102
gopts ...grpc.ServerOption) (err error) {
101103
logger := defaultLog.New(ioutil.Discard, "etcdhttp", 0)
102104

105+
// Make sure serversC is closed even if we prematurely exit the function.
106+
defer sctx.close()
107+
103108
select {
104109
case <-s.StoppingNotify():
105110
return errors.New("server is stopping")
@@ -119,8 +124,6 @@ func (sctx *serveCtx) serve(
119124
servElection := v3election.NewElectionServer(v3c)
120125
servLock := v3lock.NewLockServer(v3c)
121126

122-
// Make sure serversC is closed even if we prematurely exit the function.
123-
defer close(sctx.serversC)
124127
var gwmux *gw.ServeMux
125128
if s.Cfg.EnableGRPCGateway {
126129
// GRPC gateway connects to grpc server via connection provided by grpc dial.
@@ -503,3 +506,9 @@ func (sctx *serveCtx) registerTrace() {
503506
evf := func(w http.ResponseWriter, r *http.Request) { trace.RenderEvents(w, r, true) }
504507
sctx.registerUserHandler("/debug/events", http.HandlerFunc(evf))
505508
}
509+
510+
func (sctx *serveCtx) close() {
511+
sctx.closeOnce.Do(func() {
512+
close(sctx.serversC)
513+
})
514+
}

server/etcdserver/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,7 @@ func (s *EtcdServer) publish(timeout time.Duration) {
21302130
Val: string(b),
21312131
}
21322132

2133+
// gofail: var beforePublishing struct{}
21332134
for {
21342135
ctx, cancel := context.WithTimeout(s.ctx, timeout)
21352136
_, err := s.Do(ctx, req)

tests/integration/embed/embed_test.go

+56-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ import (
3030
"testing"
3131
"time"
3232

33+
"github.com/stretchr/testify/require"
3334
"go.etcd.io/etcd/client/pkg/v3/testutil"
3435
"go.etcd.io/etcd/client/pkg/v3/transport"
35-
"go.etcd.io/etcd/client/v3"
36+
clientv3 "go.etcd.io/etcd/client/v3"
3637
"go.etcd.io/etcd/server/v3/embed"
3738
"go.etcd.io/etcd/tests/v3/integration"
39+
gofail "go.etcd.io/gofail/runtime"
3840
)
3941

4042
var (
@@ -210,3 +212,56 @@ func setupEmbedCfg(cfg *embed.Config, curls []url.URL, purls []url.URL) {
210212
}
211213
cfg.InitialCluster = cfg.InitialCluster[1:]
212214
}
215+
216+
func TestEmbedEtcdStopDuringBootstrapping(t *testing.T) {
217+
if len(gofail.List()) == 0 {
218+
t.Skip("please run 'make gofail-enable' before running the test")
219+
}
220+
221+
fpName := "beforePublishing"
222+
require.NoError(t, gofail.Enable(fpName, `sleep("2s")`))
223+
t.Cleanup(func() {
224+
terr := gofail.Disable(fpName)
225+
if terr != nil && terr != gofail.ErrDisabled {
226+
t.Fatalf("failed to disable %s: %v", fpName, terr)
227+
}
228+
})
229+
230+
done := make(chan struct{})
231+
go func() {
232+
defer close(done)
233+
234+
cfg := embed.NewConfig()
235+
urls := newEmbedURLs(false, 2)
236+
setupEmbedCfg(cfg, []url.URL{urls[0]}, []url.URL{urls[1]})
237+
cfg.Dir = filepath.Join(t.TempDir(), "embed-etcd")
238+
239+
e, err := embed.StartEtcd(cfg)
240+
if err != nil {
241+
t.Errorf("Failed to start etcd, got error %v", err)
242+
}
243+
defer e.Close()
244+
245+
go func() {
246+
time.Sleep(time.Second)
247+
e.Server.Stop()
248+
t.Log("Stopped server during bootstrapping")
249+
}()
250+
251+
select {
252+
case <-e.Server.ReadyNotify():
253+
t.Log("Server is ready!")
254+
case <-e.Server.StopNotify():
255+
t.Log("Server is stopped")
256+
case <-time.After(20 * time.Second):
257+
e.Server.Stop() // trigger a shutdown
258+
t.Error("Server took too long to start!")
259+
}
260+
}()
261+
262+
select {
263+
case <-done:
264+
case <-time.After(10 * time.Second):
265+
t.Error("timeout in bootstrapping etcd")
266+
}
267+
}

0 commit comments

Comments
 (0)