Skip to content

Commit 5f19fc7

Browse files
authored
Make leaderResignedChan buffered to protect against hang (#1205)
This one's aimed at fixing an intermittently failing tests that we're occasionally seeing in CI: https://github.com/riverqueue/river/actions/runs/24312334093/job/70984031771?pr=1203 It was possible for the test case to end to `leaderResignedChan` before `keepLeadershipLoop` entered its select, which with an unbuffered channel, could block forever. Changing this to a buffered channel lets `keepLeadershipLoop` consume the value as its leisure while making sure the send never blocks.
1 parent 3c62677 commit 5f19fc7

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

internal/leadership/elector.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ func (e *Elector) Start(ctx context.Context) error {
136136
return nil
137137
}
138138

139-
// We'll send to this channel anytime a leader resigns on the key with `name`
140-
e.leaderResignedChan = make(chan struct{})
139+
// We'll send to this channel anytime a leader resigns on the key with `name`.
140+
// Buffered to 1 so a send doesn't block if the receiving loop hasn't entered
141+
// its select yet (e.g. between gaining and maintaining leadership).
142+
e.leaderResignedChan = make(chan struct{}, 1)
141143

142144
// Buffered to 1 so a send from handleLeadershipNotification doesn't block
143145
// if keepLeadershipLoop hasn't entered its select yet.

0 commit comments

Comments
 (0)