Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 44f1597

Browse files
Blagoj Atanasovskiatanasovskib
authored andcommitted
Fix race conditions in elector and advisory lock
1 parent 232756e commit 44f1597

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pkg/util/election.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (e *Elector) Resign() error {
7575
type ScheduledElector struct {
7676
Elector
7777
ticker *time.Ticker
78+
lock sync.RWMutex
7879
pausedScheduledElection bool
7980
}
8081

@@ -86,14 +87,20 @@ func NewScheduledElector(election Election, electionInterval time.Duration) *Sch
8687
}
8788

8889
func (se *ScheduledElector) pauseScheduledElection() {
90+
se.lock.Lock()
91+
defer se.lock.Unlock()
8992
se.pausedScheduledElection = true
9093
}
9194

9295
func (se *ScheduledElector) resumeScheduledElection() {
96+
se.lock.Lock()
97+
defer se.lock.Unlock()
9398
se.pausedScheduledElection = false
9499
}
95100

96101
func (se *ScheduledElector) isScheduledElectionPaused() bool {
102+
se.lock.RLock()
103+
defer se.lock.RUnlock()
97104
return se.pausedScheduledElection
98105
}
99106

@@ -125,7 +132,7 @@ func (se *ScheduledElector) PrometheusLivenessCheck(lastRequestUnixNano int64, t
125132

126133
func (se *ScheduledElector) scheduledElection() {
127134
for range se.ticker.C {
128-
if !se.pausedScheduledElection {
135+
if !se.isScheduledElectionPaused() {
129136
se.elect()
130137
} else {
131138
log.Debug("msg", "Scheduled election is paused. Instance can't become a leader until scheduled election is resumed (Prometheus comes up again)")

pkg/util/lock.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ func (l *PgAdvisoryLock) connCleanUp() {
154154

155155
//Close cleans up the connection
156156
func (l *PgAdvisoryLock) Close() {
157+
l.mutex.RLock()
158+
defer l.mutex.RUnlock()
157159
l.connCleanUp()
158160
}
159161

0 commit comments

Comments
 (0)