From 18382aa234f2ed2393ada6262915a49f6b89c021 Mon Sep 17 00:00:00 2001 From: Piotr Tabor Date: Tue, 16 Mar 2021 22:20:00 +0100 Subject: [PATCH] Fix 2 sources of leaked memory: embed server HTTP & v3_snapshot.leasser. --- etcdctl/snapshot/v3_snapshot.go | 6 +++--- server/embed/etcd.go | 12 ++++-------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/etcdctl/snapshot/v3_snapshot.go b/etcdctl/snapshot/v3_snapshot.go index 9594bdcb6..0773ab7bd 100644 --- a/etcdctl/snapshot/v3_snapshot.go +++ b/etcdctl/snapshot/v3_snapshot.go @@ -355,14 +355,17 @@ func (s *v3Manager) saveDB() error { // update consistentIndex so applies go through on etcdserver despite // having a new raft instance be := backend.NewDefaultBackend(dbpath) + defer be.Close() ci := cindex.NewConsistentIndex(be.BatchTx()) ci.SetConsistentIndex(uint64(commit)) // a lessor never timeouts leases lessor := lease.NewLessor(s.lg, be, lease.LessorConfig{MinLeaseTTL: math.MaxInt64}, ci) + defer lessor.Stop() mvs := mvcc.NewStore(s.lg, be, lessor, ci, mvcc.StoreConfig{CompactionBatchLimit: math.MaxInt32}) + defer mvs.Close() txn := mvs.Write(traceutil.TODO()) btx := be.BatchTx() del := func(k, v []byte) error { @@ -380,9 +383,6 @@ func (s *v3Manager) saveDB() error { txn.End() mvs.Commit() - mvs.Close() - be.Close() - return nil } diff --git a/server/embed/etcd.go b/server/embed/etcd.go index 73ef8bfb8..01d5f6916 100644 --- a/server/embed/etcd.go +++ b/server/embed/etcd.go @@ -386,18 +386,14 @@ func (e *Etcd) Close() { } func stopServers(ctx context.Context, ss *servers) { - shutdownNow := func() { - // first, close the http.Server - ss.http.Shutdown(ctx) - // then close grpc.Server; cancels all active RPCs - ss.grpc.Stop() - } + // first, close the http.Server + ss.http.Shutdown(ctx) // do not grpc.Server.GracefulStop with TLS enabled etcd server // See https://github.com/grpc/grpc-go/issues/1384#issuecomment-317124531 // and https://github.com/etcd-io/etcd/issues/8916 if ss.secure { - shutdownNow() + ss.grpc.Stop() return } @@ -415,7 +411,7 @@ func stopServers(ctx context.Context, ss *servers) { case <-ctx.Done(): // took too long, manually close open transports // e.g. watch streams - shutdownNow() + ss.grpc.Stop() // concurrent GracefulStop should be interrupted <-ch