From a2e9dc8cc0c68dad9b5ae9ea92fea78f496bfd64 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Wed, 20 Dec 2023 11:31:43 +0100 Subject: [PATCH 1/2] Don't redeclare err and snapshot variable, fixing validation of consistent index and closing database on defer `err` variable shared throughout the NewServer function and used on line 396 to defer decision whether backend should be closed when starting the server failed. `snapshot` variable is first defined 407, redeclared locally on line 496 and later again used on line 625. Creation of local variable is a bug introduced in https://github.com/etcd-io/etcd/pull/11888. Signed-off-by: Marek Siarkowicz --- server/etcdserver/server.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server/etcdserver/server.go b/server/etcdserver/server.go index 82576a5f87a..4c068c11a59 100644 --- a/server/etcdserver/server.go +++ b/server/etcdserver/server.go @@ -35,6 +35,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/prometheus/client_golang/prometheus" "go.etcd.io/etcd/server/v3/config" + "go.etcd.io/etcd/server/v3/wal/walpb" "go.uber.org/zap" pb "go.etcd.io/etcd/api/v3/etcdserverpb" @@ -485,13 +486,14 @@ func NewServer(cfg config.ServerConfig) (srv *EtcdServer, err error) { } // Find a snapshot to start/restart a raft node - walSnaps, err := wal.ValidSnapshotEntries(cfg.Logger, cfg.WALDir()) + var walSnaps []walpb.Snapshot + walSnaps, err = wal.ValidSnapshotEntries(cfg.Logger, cfg.WALDir()) if err != nil { return nil, err } // snapshot files can be orphaned if etcd crashes after writing them but before writing the corresponding // wal log entries - snapshot, err := ss.LoadNewestAvailable(walSnaps) + snapshot, err = ss.LoadNewestAvailable(walSnaps) if err != nil && err != snap.ErrNoSnapshot { return nil, err } From d6d263ac8d3b7ad650dbd7ae483e90b9e6e2d8dc Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Wed, 20 Dec 2023 11:41:50 +0100 Subject: [PATCH 2/2] Check if be is nil to avoid panic when be is overriden with nil by recoverSnapshotBackend on line 517 Signed-off-by: Marek Siarkowicz --- server/etcdserver/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/etcdserver/server.go b/server/etcdserver/server.go index 4c068c11a59..32e1e280f26 100644 --- a/server/etcdserver/server.go +++ b/server/etcdserver/server.go @@ -393,7 +393,7 @@ func NewServer(cfg config.ServerConfig) (srv *EtcdServer, err error) { } defer func() { - if err != nil { + if be != nil && err != nil { be.Close() } }()