What changed, and why it matters
This commit fixes a bug where LNbits, when connected to an LND Lightning node, would leave a payment stuck in 'pending' status if LND rejected the payment because it was a self-payment (paying your own node). The fix tells LNbits to recognize LND's 'self-payments not allowed' message as a definite pre-dispatch failure, so the payment is correctly marked as failed and the user's balance is restored instead of being frozen.
Review whether other LND error messages (and other wallet backends) have similar pre-dispatch failure strings that are not yet classified as failed. Consider backporting this fix if prior releases are affected, since pending-balance bugs can degrade availability of funds.
Security signals we found
Fixes a state-handling bug that could leave user funds locked in a pending payment
Adds regression tests for the specific failure mode
Targets pre-dispatch error classification for LND self-payment rejection
Evidence from the diff
The patch adds ‘self-payments not allowed’ to the _PRE_DISPATCH_PAYMENT_ERROR_MESSAGES tuple in both lndgrpc.py and lndrest.py. This causes the wallet abstraction to classify the payment as failed before it is dispatched, rather than leaving it in a pending/unknown state. It also adds regression tests covering both REST and gRPC LND backends, verifying that a same-node invoice returns HTTP 520, the payment is marked failed, and the wallet balance is unchanged.
Changed components
lnbits/wallets/lndgrpc.pylnbits/wallets/lndrest.pytests/regtest/test_real_invoice.pytests/unit/test_wallet_payment_ambiguity.pyInspect captured patch +62 / −0
diff --git a/lnbits/wallets/lndgrpc.py b/lnbits/wallets/lndgrpc.py
index 2641de2..aa101b5 100644
--- a/lnbits/wallets/lndgrpc.py
+++ b/lnbits/wallets/lndgrpc.py
@@ -394,6 +394,7 @@ _PRE_DISPATCH_PAYMENT_ERROR_CODES = {
_PRE_DISPATCH_PAYMENT_ERROR_MESSAGES = (
"invoice not for current active network",
"invoice expired",
+ "self-payments not allowed",
)
diff --git a/lnbits/wallets/lndrest.py b/lnbits/wallets/lndrest.py
index bec06bf..8becd03 100644
--- a/lnbits/wallets/lndrest.py
+++ b/lnbits/wallets/lndrest.py
@@ -30,6 +30,7 @@ _PRE_DISPATCH_PAYMENT_ERROR_CODES = {3, 7, 16}
_PRE_DISPATCH_PAYMENT_ERROR_MESSAGES = (
"invoice not for current active network",
"invoice expired",
+ "self-payments not allowed",
)
diff --git a/tests/regtest/test_real_invoice.py b/tests/regtest/test_real_invoice.py
index 602f2e3..0a05661 100644
--- a/tests/regtest/test_real_invoice.py
+++ b/tests/regtest/test_real_invoice.py
@@ -19,6 +19,7 @@ from lnbits.core.services.payments import (
)
from lnbits.core.services.users import create_user_account
from lnbits.exceptions import PaymentError
+from lnbits.settings import Settings
from lnbits.task_manager import task_manager
from lnbits.wallets import get_funding_source
@@ -106,6 +107,63 @@ async def test_pay_real_invoice_noroute(
assert payment["status"] == "failed"
+@pytest.mark.anyio
+@pytest.mark.skipif(
+ get_funding_source().__class__.__name__ not in ("LndRestWallet", "LndWallet"),
+ reason="LND self-payment regression requires an LND funding source",
+)
+async def test_pay_real_invoice_self_payment_is_failed(
+ client,
+ from_wallet,
+ adminkey_headers_from,
+ inkey_headers_from,
+ settings: Settings,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """A same-node invoice rejected by LND must not stay pending (#4240)."""
+ monkeypatch.setattr(settings, "lnd_rest_allow_self_payment", False)
+ monkeypatch.setattr(settings, "lnd_grpc_allow_self_payment", False)
+
+ # Create directly on the funding source so LNbits cannot pay it internally.
+ funding_source = get_funding_source()
+ invoice = await funding_source.create_invoice(amount=100, memo="regression #4240")
+ assert invoice.ok, invoice.error_message
+ assert invoice.payment_request
+ payment_hash = bolt11.decode(invoice.payment_request).payment_hash
+ assert await get_standalone_payment(payment_hash) is None
+
+ wallet_before = await get_wallet(from_wallet.id)
+ assert wallet_before
+
+ response = await client.post(
+ "/api/v1/payments",
+ json={"out": True, "bolt11": invoice.payment_request},
+ headers=adminkey_headers_from,
+ )
+ assert response.status_code == 520, response.text
+ assert response.json()["status"] == "failed"
+
+ payment = await get_standalone_payment(payment_hash, wallet_id=from_wallet.id)
+ assert payment
+ assert payment.is_out
+ assert payment.failed
+
+ response = await client.get(
+ f"/api/v1/payments/{payment_hash}", headers=inkey_headers_from
+ )
+ assert response.status_code == 200, response.text
+ assert response.json()["paid"] is False
+ assert response.json()["status"] == "failed"
+
+ payment_after = await get_standalone_payment(payment_hash, wallet_id=from_wallet.id)
+ assert payment_after
+ assert payment_after.failed
+
+ wallet_after = await get_wallet(from_wallet.id)
+ assert wallet_after
+ assert wallet_after.balance_msat == wallet_before.balance_msat
+
+
@pytest.mark.anyio
@pytest.mark.skipif(is_fake, reason="this only works in regtest")
async def test_pay_real_invoice_mainnet(
diff --git a/tests/unit/test_wallet_payment_ambiguity.py b/tests/unit/test_wallet_payment_ambiguity.py
index 9c23d32..001a99d 100644
--- a/tests/unit/test_wallet_payment_ambiguity.py
+++ b/tests/unit/test_wallet_payment_ambiguity.py
@@ -164,6 +164,7 @@ async def test_lndrest_unknown_payment_state_is_pending(
False,
),
({"code": 2, "message": "invoice expired"}, False),
+ ({"code": 2, "message": "self-payments not allowed"}, False),
({"code": 3, "message": "invalid payment request"}, False),
({"code": 2, "message": "payment stream interrupted"}, None),
({"code": 14, "message": "transport unavailable"}, None),
@@ -200,6 +201,7 @@ async def test_lndrest_only_pre_dispatch_rpc_errors_are_failed(
False,
),
(grpc.StatusCode.UNKNOWN, "invoice expired", False),
+ (grpc.StatusCode.UNKNOWN, "self-payments not allowed", False),
(grpc.StatusCode.INVALID_ARGUMENT, "invalid payment request", False),
(grpc.StatusCode.PERMISSION_DENIED, "permission denied", False),
(grpc.StatusCode.UNAUTHENTICATED, "invalid macaroon", False),
Why this scored 60/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.