Skip to content

Commit 4eca8b7

Browse files
committed
listen for ctx expiry when sending spend and conf
Avoid a deadlock in case the caller doesn't consume notifications about spending or confirmations after cancelling a call.
1 parent 692014f commit 4eca8b7

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

chainnotifier_client.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
159159
if err != nil {
160160
return err
161161
}
162-
spendChan <- &chainntnfs.SpendDetail{
162+
spend := &chainntnfs.SpendDetail{
163163
SpentOutPoint: &wire.OutPoint{
164164
Hash: *outpointHash,
165165
Index: d.SpendingOutpoint.Index,
@@ -170,7 +170,12 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
170170
SpendingHeight: int32(d.SpendingHeight),
171171
}
172172

173-
return nil
173+
select {
174+
case spendChan <- spend:
175+
return nil
176+
case <-ctx.Done():
177+
return ctx.Err()
178+
}
174179
}
175180

176181
processReorg := func() {
@@ -302,14 +307,20 @@ func (s *chainNotifierClient) RegisterConfirmationsNtfn(ctx context.Context,
302307
return
303308
}
304309

305-
confChan <- &chainntnfs.TxConfirmation{
310+
conf := &chainntnfs.TxConfirmation{
306311
BlockHeight: c.Conf.BlockHeight,
307312
BlockHash: blockHash,
308313
Tx: tx,
309314
TxIndex: c.Conf.TxIndex,
310315
Block: block,
311316
}
312317

318+
select {
319+
case confChan <- conf:
320+
case <-ctx.Done():
321+
return
322+
}
323+
313324
// If we're running in re-org aware mode, then
314325
// we don't return here, since we might want to
315326
// be informed about the new block we got

0 commit comments

Comments
 (0)