Skip to content

Commit

Permalink
routing: fix nil pointer dereference in exitWithErr
Browse files Browse the repository at this point in the history
In a rare case when the critical log is triggered when using postgres as
db backend, the `payment` could be nil cause the server is shutting
down, causing the payment fetching to return an error. We now cache its
state before fetching it from the db.
  • Loading branch information
yyforyongyu committed Dec 16, 2024
1 parent 944f162 commit 39584be
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions routing/payment_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,21 @@ func (p *paymentLifecycle) resumePayment(ctx context.Context) ([32]byte,
p.resultCollector(&a)
}

// Get the payment status.
status := payment.GetStatus()

// exitWithErr is a helper closure that logs and returns an error.
exitWithErr := func(err error) ([32]byte, *route.Route, error) {
log.Errorf("Payment %v with status=%v failed: %v",
p.identifier, payment.GetStatus(), err)
// Log an error with the latest payment status.
//
// NOTE: this `status` variable is reassigned in the loop
// below. We could also call `payment.GetStatus` here, but in a
// rare case when the critical log is triggered when using
// postgres as db backend, the `payment` could be nil, causing
// the payment fetching to return an error.
log.Errorf("Payment %v with status=%v failed: %v", p.identifier,
status, err)

return [32]byte{}, nil, err
}

Expand All @@ -213,10 +224,10 @@ lifecycle:
ps := payment.GetState()
remainingFees := p.calcFeeBudget(ps.FeesPaid)

status = payment.GetStatus()
log.Debugf("Payment %v: status=%v, active_shards=%v, "+
"rem_value=%v, fee_limit=%v", p.identifier,
payment.GetStatus(), ps.NumAttemptsInFlight,
ps.RemainingAmt, remainingFees)
"rem_value=%v, fee_limit=%v", p.identifier, status,
ps.NumAttemptsInFlight, ps.RemainingAmt, remainingFees)

// We now proceed our lifecycle with the following tasks in
// order,
Expand Down

0 comments on commit 39584be

Please sign in to comment.