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

kvserver: fix race in rangefeed processor setup #53295

Merged
Merged
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
24 changes: 14 additions & 10 deletions pkg/kv/kvserver/replica_rangefeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package kvserver
import (
"context"
"fmt"
"sync"
"time"

"github.com/cockroachdb/cockroach/pkg/base"
Expand Down Expand Up @@ -170,14 +171,19 @@ func (r *Replica) RangeFeed(
if err := lim.Begin(ctx); err != nil {
return roachpb.NewError(err)
}
// Finish the iterator limit, but only if we exit before
// creating the iterator itself.
iterSemRelease = lim.Finish
defer func() {
if iterSemRelease != nil {
iterSemRelease()
}
}()
// Finish the iterator limit if we exit before the iterator finishes.
// The release function will be hooked into the Close method on the
// iterator below. The sync.Once prevents any races between exiting early
// from this call and finishing the catchup scan underneath the
// rangefeed.Processor. We need to release here in case we fail to
// register the processor, or, more perniciously, in the case where the
// processor gets registered by shut down before starting the catchup
// scan.
var iterSemReleaseOnce sync.Once
iterSemRelease = func() {
iterSemReleaseOnce.Do(lim.Finish)
}
defer iterSemRelease()
}

// Lock the raftMu, then register the stream as a new rangefeed registration.
Expand Down Expand Up @@ -210,8 +216,6 @@ func (r *Replica) RangeFeed(
SimpleIterator: innerIter,
close: iterSemRelease,
}
// Responsibility for releasing the semaphore now passes to the iterator.
iterSemRelease = nil
return catchUpIter
}
}
Expand Down