@@ -364,11 +364,18 @@ func (p *paymentLifecycle) checkContext(ctx context.Context) error {
364364 p .identifier .String ())
365365 }
366366
367+ // The context is already cancelled at this point, so we create
368+ // a new context so the payment can successfully be marked as
369+ // failed.
370+ cleanupCtx := context .WithoutCancel (ctx )
371+
367372 // By marking the payment failed, depending on whether it has
368373 // inflight HTLCs or not, its status will now either be
369374 // `StatusInflight` or `StatusFailed`. In either case, no more
370375 // HTLCs will be attempted.
371- err := p .router .cfg .Control .FailPayment (p .identifier , reason )
376+ err := p .router .cfg .Control .FailPayment (
377+ cleanupCtx , p .identifier , reason ,
378+ )
372379 if err != nil {
373380 return fmt .Errorf ("FailPayment got %w" , err )
374381 }
@@ -389,6 +396,8 @@ func (p *paymentLifecycle) checkContext(ctx context.Context) error {
389396func (p * paymentLifecycle ) requestRoute (
390397 ps * paymentsdb.MPPaymentState ) (* route.Route , error ) {
391398
399+ ctx := context .TODO ()
400+
392401 remainingFees := p .calcFeeBudget (ps .FeesPaid )
393402
394403 // Query our payment session to construct a route.
@@ -430,7 +439,9 @@ func (p *paymentLifecycle) requestRoute(
430439 log .Warnf ("Marking payment %v permanently failed with no route: %v" ,
431440 p .identifier , failureCode )
432441
433- err = p .router .cfg .Control .FailPayment (p .identifier , failureCode )
442+ err = p .router .cfg .Control .FailPayment (
443+ ctx , p .identifier , failureCode ,
444+ )
434445 if err != nil {
435446 return nil , fmt .Errorf ("FailPayment got: %w" , err )
436447 }
@@ -800,6 +811,8 @@ func (p *paymentLifecycle) failPaymentAndAttempt(
800811 attemptID uint64 , reason * paymentsdb.FailureReason ,
801812 sendErr error ) (* attemptResult , error ) {
802813
814+ ctx := context .TODO ()
815+
803816 log .Errorf ("Payment %v failed: final_outcome=%v, raw_err=%v" ,
804817 p .identifier , * reason , sendErr )
805818
@@ -808,7 +821,9 @@ func (p *paymentLifecycle) failPaymentAndAttempt(
808821 // NOTE: we must fail the payment first before failing the attempt.
809822 // Otherwise, once the attempt is marked as failed, another goroutine
810823 // might make another attempt while we are failing the payment.
811- err := p .router .cfg .Control .FailPayment (p .identifier , * reason )
824+ err := p .router .cfg .Control .FailPayment (
825+ ctx , p .identifier , * reason ,
826+ )
812827 if err != nil {
813828 log .Errorf ("Unable to fail payment: %v" , err )
814829 return nil , err
0 commit comments