Skip to content

Commit

Permalink
fix: Unsubscribe required on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Aug 17, 2023
1 parent 254a9cb commit 5f219aa
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/chain/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,17 @@ func (store *Store) SubHeadChanges(ctx context.Context) chan []*types.HeadChange
}}

go func() {
defer close(out)
var unsubOnce sync.Once
defer func() {
// Tell the caller we're done first, the following may block for a bit.
close(out)

// Unsubscribe.
store.headEvents.Unsub(subCh)

// Drain the channel.
for range subCh {
}
}()

for {
select {
Expand All @@ -716,9 +725,8 @@ func (store *Store) SubHeadChanges(ctx context.Context) chan []*types.HeadChange
log.Warnf("head change sub is slow, has %d buffered entries", len(out))
}
case <-ctx.Done():
unsubOnce.Do(func() {
go store.headEvents.Unsub(subCh)
})
log.Infof("exit sub head change: %v", ctx.Err())
return
}
}
}()
Expand Down

0 comments on commit 5f219aa

Please sign in to comment.