Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid race condition in memory topo watch shutdown #10954

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go/vt/topo/memorytopo/memorytopo.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ func (c *Conn) dial(ctx context.Context) error {
// Close is part of the topo.Conn interface.
// It nils out factory, so any subsequent call will panic.
func (c *Conn) Close() {
c.factory.Lock()
f := c.factory
defer f.Unlock()
c.factory = nil
}

Expand Down
22 changes: 12 additions & 10 deletions go/vt/topo/memorytopo/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (

// Watch is part of the topo.Conn interface.
func (c *Conn) Watch(ctx context.Context, filePath string) (*topo.WatchData, <-chan *topo.WatchData, error) {
c.factory.mu.Lock()
defer c.factory.mu.Unlock()
c.factory.Lock()
defer c.factory.Unlock()

if c.factory.err != nil {
return nil, nil, c.factory.err
Expand Down Expand Up @@ -54,10 +54,11 @@ func (c *Conn) Watch(ctx context.Context, filePath string) (*topo.WatchData, <-c
<-ctx.Done()
// This function can be called at any point, so we first need
// to make sure the watch is still valid.
c.factory.mu.Lock()
defer c.factory.mu.Unlock()
c.factory.Lock()
f := c.factory
defer f.Unlock()

n := c.factory.nodeByPath(c.cell, filePath)
n := f.nodeByPath(c.cell, filePath)
if n == nil {
return
}
Expand All @@ -73,8 +74,8 @@ func (c *Conn) Watch(ctx context.Context, filePath string) (*topo.WatchData, <-c

// WatchRecursive is part of the topo.Conn interface.
func (c *Conn) WatchRecursive(ctx context.Context, dirpath string) ([]*topo.WatchDataRecursive, <-chan *topo.WatchDataRecursive, error) {
c.factory.mu.Lock()
defer c.factory.mu.Unlock()
c.factory.Lock()
defer c.factory.Unlock()

if c.factory.err != nil {
return nil, nil, c.factory.err
Expand Down Expand Up @@ -106,10 +107,11 @@ func (c *Conn) WatchRecursive(ctx context.Context, dirpath string) ([]*topo.Watc

<-ctx.Done()

c.factory.mu.Lock()
defer c.factory.mu.Unlock()
c.factory.Lock()
f := c.factory
defer f.Unlock()

n := c.factory.nodeByPath(c.cell, dirpath)
n := f.nodeByPath(c.cell, dirpath)
if n != nil {
delete(n.watches, watchIndex)
}
Expand Down