Skip to content

Commit

Permalink
clientV3watch: Watch Close should close successfully
Browse files Browse the repository at this point in the history
Closing of watch by client will cancel the watch grpc stream and
can produce a context canceled error. However, since client
simply wanted to close the watcher the error can create confusion
that something went wrong instead of a successful close. Ensure
that Close do not return error.

Fixed etcd-io#10340
  • Loading branch information
spzala committed Mar 1, 2019
1 parent 25068df commit b25edb6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions clientv3/integration/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,3 +1133,24 @@ func TestWatchCancelDisconnected(t *testing.T) {
t.Fatal("took too long to cancel disconnected watcher")
}
}

// TestWatchClose ensures that close does not return error
func TestWatchClose(t *testing.T) {
runWatchTest(t, testWatchClose)
}

func testWatchClose(t *testing.T, wctx *watchctx) {
ctx, cancel := context.WithCancel(context.Background())
wch := wctx.w.Watch(ctx, "a")
cancel()
if wch == nil {
t.Fatalf("expected watcher channel, got nil")
}
if wctx.w.Close() != nil {
t.Fatalf("watch did not close successfully")
}
wresp, ok := <-wch
if ok {
t.Fatalf("read wch got %v; expected closed channel", wresp)
}
}
4 changes: 4 additions & 0 deletions clientv3/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ func (w *watcher) Close() (err error) {
err = werr
}
}
// Consider context.Canceled as a successful close
if err == context.Canceled {
err = nil
}
return err
}

Expand Down

0 comments on commit b25edb6

Please sign in to comment.