From b749e28b1a4a8f0a5ad3583eb4fd7d5e54b889b1 Mon Sep 17 00:00:00 2001 From: "Asim A." Date: Tue, 27 Oct 2026 10:37:49 +0000 Subject: [PATCH] fix(etcd): skip deletes without previous value (#5040) Co-authored-by: Codex --- registry/etcd/watcher.go | 3 +++ registry/etcd/watcher_test.go | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 registry/etcd/watcher_test.go diff --git a/registry/etcd/watcher.go b/registry/etcd/watcher.go index 5d6e1f751e..cf8acb4bec 100644 --- a/registry/etcd/watcher.go +++ b/registry/etcd/watcher.go @@ -66,6 +66,9 @@ func (ew *etcdWatcher) Next() (*registry.Result, error) { action = "delete" // get service from prevKv + if ev.PrevKv == nil { + continue + } service = decode(ev.PrevKv.Value) } diff --git a/registry/etcd/watcher_test.go b/registry/etcd/watcher_test.go new file mode 100644 index 0000000000..c09bc6a724 --- /dev/null +++ b/registry/etcd/watcher_test.go @@ -0,0 +1,41 @@ +package etcd + +import ( + "testing" + + "go-micro.dev/v5/registry" + "go.etcd.io/etcd/api/v3/mvccpb" + clientv3 "go.etcd.io/etcd/client/v3" +) + +func TestWatcherSkipsDeleteWithoutPrevKV(t *testing.T) { + service := ®istry.Service{Name: "example", Version: "1.0.0"} + watchCh := make(chan clientv3.WatchResponse, 1) + watchCh <- clientv3.WatchResponse{Events: []*clientv3.Event{ + { + Type: mvccpb.DELETE, + Kv: &mvccpb.KeyValue{Key: []byte("missing-prev-kv")}, + }, + { + Type: mvccpb.DELETE, + Kv: &mvccpb.KeyValue{Key: []byte("valid-delete")}, + PrevKv: &mvccpb.KeyValue{Value: []byte(encode(service))}, + }, + }} + + watcher := &etcdWatcher{ + stop: make(chan bool, 1), + w: watchCh, + } + + result, err := watcher.Next() + if err != nil { + t.Fatalf("Next() returned an unexpected error: %v", err) + } + if result.Action != "delete" { + t.Fatalf("Next() action = %q, want %q", result.Action, "delete") + } + if result.Service.Name != service.Name || result.Service.Version != service.Version { + t.Fatalf("Next() service = %#v, want %#v", result.Service, service) + } +}