multi: thread context through DeletePayments
What changed, and why it matters
This change threads a request-scoped cancellation signal (called a 'context') through the DeletePayments database operation. Previously the SQL backend used a placeholder context.TODO(), which meant long-running payment deletion requests could not be cancelled by the caller. The change itself is a straightforward plumbing improvement and does not appear to fix an exploitable vulnerability.
No immediate security action required. Treat as a normal reliability/maintenance improvement. Review whether other long-running SQL store methods still use context.TODO() and consider similar context plumbing.
Security signals we found
context propagation improvement for long-running DB operation
removal of context.TODO() placeholder in SQL backend
no change to authorization, validation, or deletion semantics
Evidence from the diff
The commit updates the PaymentWriter.DeletePayments signature to accept a context.Context and propagates it from rpcserver.DeleteAllPayments down through both the KV and SQL store implementations. In sql_store.go it replaces context.TODO() with the supplied ctx, allowing the SQL queries to honour gRPC/HTTP request cancellation and timeouts. No logic changes to access control, filtering, or deletion behaviour are present.
Changed components
payments/db/interface.gopayments/db/kv_store.gopayments/db/sql_store.gorpcserver.goInspect captured patch +19 / −14
diff --git a/payments/db/interface.go b/payments/db/interface.go
index caf222b..c6f1bf3 100644
--- a/payments/db/interface.go
+++ b/payments/db/interface.go
@@ -36,7 +36,8 @@ type PaymentWriter interface {
// DeletePayments deletes all payments from the DB given the specified
// flags.
- DeletePayments(failedOnly, failedAttemptsOnly bool) (int, error)
+ DeletePayments(ctx context.Context, failedOnly,
+ failedAttemptsOnly bool) (int, error)
PaymentControl
}
diff --git a/payments/db/kv_store.go b/payments/db/kv_store.go
index 138edb6..d3d347a 100644
--- a/payments/db/kv_store.go
+++ b/payments/db/kv_store.go
@@ -1372,7 +1372,7 @@ func (p *KVStore) DeletePayment(_ context.Context, paymentHash lntypes.Hash,
// failedHtlcsOnly is set, the payment itself won't be deleted, only failed HTLC
// attempts. The method returns the number of deleted payments, which is always
// 0 if failedHtlcsOnly is set.
-func (p *KVStore) DeletePayments(failedOnly,
+func (p *KVStore) DeletePayments(_ context.Context, failedOnly,
failedHtlcsOnly bool) (int, error) {
var numPayments int
diff --git a/payments/db/kv_store_test.go b/payments/db/kv_store_test.go
index f0c2b14..cf5a9a9 100644
--- a/payments/db/kv_store_test.go
+++ b/payments/db/kv_store_test.go
@@ -30,6 +30,8 @@ import (
func TestKVStoreDeleteNonInFlight(t *testing.T) {
t.Parallel()
+ ctx := t.Context()
+
paymentDB := NewKVTestDB(t)
// Create a sequence number for duplicate payments that will not collide
@@ -180,7 +182,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
}
// Delete all failed payments.
- numPayments, err := paymentDB.DeletePayments(true, false)
+ numPayments, err := paymentDB.DeletePayments(ctx, true, false)
require.NoError(t, err)
require.EqualValues(t, 1, numPayments)
@@ -216,7 +218,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
}
// Now delete all payments except in-flight.
- numPayments, err = paymentDB.DeletePayments(false, false)
+ numPayments, err = paymentDB.DeletePayments(ctx, false, false)
require.NoError(t, err)
require.EqualValues(t, 2, numPayments)
diff --git a/payments/db/payment_test.go b/payments/db/payment_test.go
index 1180cf9..4a9a69b 100644
--- a/payments/db/payment_test.go
+++ b/payments/db/payment_test.go
@@ -1658,6 +1658,8 @@ func TestFailsWithoutInFlight(t *testing.T) {
func TestDeletePayments(t *testing.T) {
t.Parallel()
+ ctx := t.Context()
+
paymentDB, _ := NewTestDB(t)
// Register three payments:
@@ -1678,7 +1680,7 @@ func TestDeletePayments(t *testing.T) {
assertDBPayments(t, paymentDB, payments)
// Delete HTLC attempts for failed payments only.
- numPayments, err := paymentDB.DeletePayments(true, true)
+ numPayments, err := paymentDB.DeletePayments(ctx, true, true)
require.NoError(t, err)
require.EqualValues(t, 0, numPayments)
@@ -1687,7 +1689,7 @@ func TestDeletePayments(t *testing.T) {
assertDBPayments(t, paymentDB, payments)
// Delete failed attempts for all payments.
- numPayments, err = paymentDB.DeletePayments(false, true)
+ numPayments, err = paymentDB.DeletePayments(ctx, false, true)
require.NoError(t, err)
require.EqualValues(t, 0, numPayments)
@@ -1697,14 +1699,14 @@ func TestDeletePayments(t *testing.T) {
assertDBPayments(t, paymentDB, payments)
// Now delete all failed payments.
- numPayments, err = paymentDB.DeletePayments(true, false)
+ numPayments, err = paymentDB.DeletePayments(ctx, true, false)
require.NoError(t, err)
require.EqualValues(t, 1, numPayments)
assertDBPayments(t, paymentDB, payments[1:])
// Finally delete all completed payments.
- numPayments, err = paymentDB.DeletePayments(false, false)
+ numPayments, err = paymentDB.DeletePayments(ctx, false, false)
require.NoError(t, err)
require.EqualValues(t, 1, numPayments)
diff --git a/payments/db/sql_store.go b/payments/db/sql_store.go
index f6f5d0d..ef0d96c 100644
--- a/payments/db/sql_store.go
+++ b/payments/db/sql_store.go
@@ -1874,13 +1874,13 @@ func (s *SQLStore) Fail(paymentHash lntypes.Hash,
// This method is part of the PaymentWriter interface, which is embedded in
// the DB interface.
//
-// TODO(ziggie): batch this call instead in the background so for dbs with
-// many payments it doesn't block the main thread.
-func (s *SQLStore) DeletePayments(failedOnly, failedHtlcsOnly bool) (int,
- error) {
+// TODO(ziggie): batch and use iterator instead, moreover we dont need to fetch
+// the complete payment data for each payment, we can just fetch the payment ID
+// and the resolution types to decide if the payment is removable.
+func (s *SQLStore) DeletePayments(ctx context.Context, failedOnly,
+ failedHtlcsOnly bool) (int, error) {
var numPayments int
- ctx := context.TODO()
extractCursor := func(row sqlc.FilterPaymentsRow) int64 {
return row.Payment.ID
diff --git a/rpcserver.go b/rpcserver.go
index df27c41..2f88b2d 100644
--- a/rpcserver.go
+++ b/rpcserver.go
@@ -7782,7 +7782,7 @@ func (r *rpcServer) DeleteAllPayments(ctx context.Context,
req.FailedHtlcsOnly)
numDeletedPayments, err := r.server.paymentsDB.DeletePayments(
- req.FailedPaymentsOnly, req.FailedHtlcsOnly,
+ ctx, req.FailedPaymentsOnly, req.FailedHtlcsOnly,
)
if err != nil {
return nil, err
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.