Skip to content

Commit

Permalink
*: avoid closing a watch with ID 0 incorrectly
Browse files Browse the repository at this point in the history
Signed-off-by: Kafuu Chino <KafuuChinoQ@gmail.com>
  • Loading branch information
kafuu-chino committed Aug 23, 2022
1 parent e087c34 commit e881993
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions server/etcdserver/api/v3rpc/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/mvccpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/client/pkg/v3/verify"
"go.etcd.io/etcd/server/v3/auth"
"go.etcd.io/etcd/server/v3/etcdserver"
"go.etcd.io/etcd/server/v3/etcdserver/apply"
Expand Down Expand Up @@ -270,7 +271,7 @@ func (sws *serverWatchStream) recvLoop() error {
if !sws.isWatchPermitted(creq) {
wr := &pb.WatchResponse{
Header: sws.newResponseHeader(sws.watchStream.Rev()),
WatchId: creq.WatchId,
WatchId: int64(mvcc.InvalidWatchID),
Canceled: true,
Created: true,
CancelReason: rpctypes.ErrGRPCPermissionDenied.Error(),
Expand Down Expand Up @@ -465,7 +466,10 @@ func (sws *serverWatchStream) sendLoop() {

// track id creation
wid := mvcc.WatchID(c.WatchId)
if c.Canceled {

verify.Assert((c.Canceled && c.Created) && wid != mvcc.InvalidWatchID, "the watch response invalid")

if c.Canceled && wid != mvcc.InvalidWatchID {
delete(ids, wid)
continue
}
Expand Down
11 changes: 8 additions & 3 deletions server/storage/mvcc/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ import (
"go.etcd.io/etcd/api/v3/mvccpb"
)

// AutoWatchID is the watcher ID passed in WatchStream.Watch when no
// user-provided ID is available. If pass, an ID will automatically be assigned.
const AutoWatchID WatchID = 0
const (
// InvalidWatchID represents an invalid watch ID and prevents duplication with an existing watch.
InvalidWatchID WatchID = -1

// AutoWatchID is the watcher ID passed in WatchStream.Watch when no
// user-provided ID is available. If pass, an ID will automatically be assigned.
AutoWatchID WatchID = 0
)

var (
ErrWatcherNotExist = errors.New("mvcc: watcher does not exist")
Expand Down

0 comments on commit e881993

Please sign in to comment.