Skip to content

Commit 4eb1aeb

Browse files
committed
tapchannel: improve quit handling for aux signer
In this commit, we add checks of the aux signer cancel and quit signals at all points during aux sig batch processing when a response may be sent. This mirrors the signal handling used in the lnwallet sigpool worker goroutine. We also update the early exit logic to not close the cancel channel; only the caller, lnd, should mutate that channel.
1 parent 3087b6a commit 4eb1aeb

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

tapchannel/aux_leaf_signer.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
"github.com/lightningnetwork/lnd/tlv"
2929
)
3030

31+
// shutdownErr is used in multiple spots when exiting the sig batch processor.
32+
var shutdownErr = fmt.Errorf("tapd is shutting down")
33+
3134
// VirtualPacketSigner is an interface that can be used to sign virtual packets.
3235
type VirtualPacketSigner interface {
3336
// SignVirtualPacket signs the virtual transaction of the given packet
@@ -241,43 +244,49 @@ func (s *AuxLeafSigner) processAuxSigBatch(chanState *channeldb.OpenChannel,
241244
defer s.Wg.Done()
242245

243246
log.Tracef("Processing %d aux sig jobs", len(sigJobs))
244-
245247
for idx := range sigJobs {
246248
sigJob := sigJobs[idx]
247-
cancelAndErr := func(err error) {
249+
respondErr := func(err error) {
248250
log.Errorf("Error processing aux sig job: %v", err)
249251

250-
close(sigJob.Cancel)
251252
sigJob.Resp <- lnwallet.AuxSigJobResp{
252253
Err: err,
253254
}
254255
}
255256

256-
// If we're shutting down, we cancel the job and return.
257+
// Check for cancel or quit signals before beginning the job.
257258
select {
259+
case <-sigJob.Cancel:
260+
continue
258261
case <-s.Quit:
259-
cancelAndErr(fmt.Errorf("tapd is shutting down"))
262+
respondErr(shutdownErr)
260263
return
261-
262264
default:
263265
}
264266

265267
// If there is no commit blob, this isn't a custom channel. We
266268
// still need to signal the job as done though, even if we don't
267269
// have a signature to return.
268270
if sigJob.CommitBlob.IsNone() {
269-
sigJob.Resp <- lnwallet.AuxSigJobResp{
271+
select {
272+
case sigJob.Resp <- lnwallet.AuxSigJobResp{
270273
HtlcIndex: sigJob.HTLC.HtlcIndex,
274+
}:
275+
continue
276+
case <-sigJob.Cancel:
277+
continue
278+
case <-s.Quit:
279+
respondErr(shutdownErr)
280+
return
271281
}
272-
continue
273282
}
274283

275284
com, err := cmsg.DecodeCommitment(
276285
sigJob.CommitBlob.UnsafeFromSome(),
277286
)
278287
if err != nil {
279-
cancelAndErr(fmt.Errorf("error decoding commitment: "+
280-
"%w", err))
288+
respondErr(fmt.Errorf("error decoding commitment: %w",
289+
err))
281290
return
282291
}
283292

@@ -299,26 +308,41 @@ func (s *AuxLeafSigner) processAuxSigBatch(chanState *channeldb.OpenChannel,
299308
// If the HTLC doesn't have any asset outputs, it's not an
300309
// asset HTLC, so we can skip it.
301310
if len(htlcOutputs) == 0 {
302-
sigJob.Resp <- lnwallet.AuxSigJobResp{
311+
select {
312+
case sigJob.Resp <- lnwallet.AuxSigJobResp{
303313
HtlcIndex: sigJob.HTLC.HtlcIndex,
314+
}:
315+
continue
316+
case <-sigJob.Cancel:
317+
continue
318+
case <-s.Quit:
319+
respondErr(shutdownErr)
320+
return
304321
}
305-
continue
306322
}
307323

308324
resp, err := s.generateHtlcSignature(
309325
chanState, commitTx, htlcOutputs, sigJob.SignDesc,
310326
sigJob.BaseAuxJob,
311327
)
312328
if err != nil {
313-
cancelAndErr(fmt.Errorf("error generating HTLC "+
329+
respondErr(fmt.Errorf("error generating HTLC "+
314330
"signature: %w", err))
315331
return
316332
}
317333

318334
// Success!
319335
log.Tracef("Generated HTLC signature for HTLC with index %d",
320336
sigJob.HTLC.HtlcIndex)
321-
sigJob.Resp <- resp
337+
338+
select {
339+
case sigJob.Resp <- resp:
340+
case <-sigJob.Cancel:
341+
continue
342+
case <-s.Quit:
343+
respondErr(shutdownErr)
344+
return
345+
}
322346
}
323347
}
324348

0 commit comments

Comments
 (0)