Skip to content

Commit eadb128

Browse files
committed
feat: fix potential deadlock in make primary
Signed-off-by: Manan Gupta <[email protected]>
1 parent 2c6e053 commit eadb128

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

go/vt/vttablet/tabletserver/health_streamer.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,10 @@ func (hs *healthStreamer) SetUnhealthyThreshold(v time.Duration) {
293293
// so it can read and write to the MySQL instance for schema-tracking.
294294
func (hs *healthStreamer) MakePrimary(serving bool) {
295295
hs.fieldsMu.Lock()
296-
defer hs.fieldsMu.Unlock()
297296
hs.isServingPrimary = serving
297+
// We let go of the lock here because we don't want to hold the lock when calling RegisterNotifier.
298+
// If we keep holding the lock, there is a potential deadlock that can happen.
299+
hs.fieldsMu.Unlock()
298300
// We register for notifications from the schema Engine only when schema tracking is enabled,
299301
// and we are going to a serving primary state.
300302
if serving && hs.signalWhenSchemaChange {

go/vt/vttablet/tabletserver/health_streamer_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -592,13 +592,14 @@ func TestDeadlockBwCloseAndReload(t *testing.T) {
592592

593593
wg := sync.WaitGroup{}
594594
wg.Add(2)
595-
// Try running Close and reload in parallel multiple times.
595+
// Try running Close & MakePrimary and reload in parallel multiple times.
596596
// This reproduces the deadlock quite readily.
597597
go func() {
598598
defer wg.Done()
599599
for i := 0; i < 100; i++ {
600600
hs.Close()
601601
hs.Open()
602+
hs.MakePrimary(true)
602603
}
603604
}()
604605

0 commit comments

Comments
 (0)