Skip to content

Commit

Permalink
Properly close events channel on subscription close (#9864)
Browse files Browse the repository at this point in the history
This fixes a longstanding bug where calling code expects sub.Events()
channel to be closed when the subscription is closed, but it wasn't.
  • Loading branch information
samsondav authored Jul 21, 2023
1 parent 648f328 commit 7b06aa1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/services/pg/event_broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func (sub *subscription) Close() {
if err != nil {
sub.lggr.Errorw("THIS NEVER RETURNS AN ERROR", "err", err)
}
close(sub.chEvents)
}

// NullEventBroadcaster implements null pattern for event broadcaster
Expand Down
18 changes: 18 additions & 0 deletions core/services/pg/event_broadcaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,22 @@ func TestEventBroadcaster(t *testing.T) {

wg.Wait()
})

t.Run("closes events channel on subscription close", func(t *testing.T) {
sub, err := eventBroadcaster.Subscribe("foo", "")
require.NoError(t, err)

chEvents := sub.Events()

sub.Close()

select {
case _, ok := <-chEvents:
if ok {
t.Fatal("expected chEvents to be closed")
}
default:
t.Fatal("expected chEvents to not block")
}
})
}

0 comments on commit 7b06aa1

Please sign in to comment.