Not send sentry task to closed tasks channel (#4467)

* Not send sentry task to closed tasks channel

* Add comments

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
This commit is contained in:
ledgerwatch 2022-06-16 11:17:53 +01:00 committed by GitHub
parent bca563fd0f
commit df3eea6414
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,6 +83,8 @@ func (pi *PeerInfo) Close() {
defer pi.lock.Unlock()
if pi.tasks != nil {
close(pi.tasks)
// Setting this to nil because other functions detect the closure of the channel by checking pi.tasks == nil
pi.tasks = nil
}
}
@ -144,11 +146,16 @@ func (pi *PeerInfo) Remove() {
func (pi *PeerInfo) Async(f func()) {
pi.lock.Lock()
defer pi.lock.Unlock()
if pi.tasks == nil {
// Too late, the task channel has been closed
return
}
select {
case <-pi.removed: // noop if peer removed
case <-pi.ctx.Done():
if pi.tasks != nil {
close(pi.tasks)
// Setting this to nil because other functions detect the closure of the channel by checking pi.tasks == nil
pi.tasks = nil
}
case pi.tasks <- f: