Skip to content

Commit 5449ecc

Browse files
authored
Merge pull request #6103 from filecoin-project/fix/not-unsubscribe
fix: Unsubscribe required on exit
2 parents 254a9cb + 5f219aa commit 5449ecc

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

pkg/chain/store.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,17 @@ func (store *Store) SubHeadChanges(ctx context.Context) chan []*types.HeadChange
695695
}}
696696

697697
go func() {
698-
defer close(out)
699-
var unsubOnce sync.Once
698+
defer func() {
699+
// Tell the caller we're done first, the following may block for a bit.
700+
close(out)
701+
702+
// Unsubscribe.
703+
store.headEvents.Unsub(subCh)
704+
705+
// Drain the channel.
706+
for range subCh {
707+
}
708+
}()
700709

701710
for {
702711
select {
@@ -716,9 +725,8 @@ func (store *Store) SubHeadChanges(ctx context.Context) chan []*types.HeadChange
716725
log.Warnf("head change sub is slow, has %d buffered entries", len(out))
717726
}
718727
case <-ctx.Done():
719-
unsubOnce.Do(func() {
720-
go store.headEvents.Unsub(subCh)
721-
})
728+
log.Infof("exit sub head change: %v", ctx.Err())
729+
return
722730
}
723731
}
724732
}()

0 commit comments

Comments
 (0)