diff --git a/clientv3/client.go b/clientv3/client.go index bc66a57c4..d6000a85c 100644 --- a/clientv3/client.go +++ b/clientv3/client.go @@ -129,8 +129,12 @@ func NewFromURLs(urls []string) (*Client, error) { // Close shuts down the client's etcd connections. func (c *Client) Close() error { c.cancel() - c.Watcher.Close() - c.Lease.Close() + if c.Watcher != nil { + c.Watcher.Close() + } + if c.Lease != nil { + c.Lease.Close() + } if c.resolverGroup != nil { c.resolverGroup.Close() } diff --git a/clientv3/client_test.go b/clientv3/client_test.go index 47544a0da..de7c59229 100644 --- a/clientv3/client_test.go +++ b/clientv3/client_test.go @@ -156,3 +156,13 @@ func TestIsHaltErr(t *testing.T) { t.Errorf("cancel on context should be Halted") } } + +func TestCloseCtxClient(t *testing.T) { + ctx := context.Background() + c := NewCtxClient(ctx) + err := c.Close() + // Close returns ctx.toErr, a nil error means an open Done channel + if err == nil { + t.Errorf("failed to Close the client. %v", err) + } +}