fix: exponential backoff for IN_FLIGHT payment polling (#3918)
What changed, and why it matters
This patch fixes how LNbits polls the LND lightning node about payments that are still 'in flight' (not yet succeeded or failed). Previously, the code asked for an endless streaming connection with no timeout and immediately returned 'pending' as soon as it saw one IN_FLIGHT update, which could leave long-lived connections hanging and might not correctly reflect the final payment outcome. The change caps each poll at 30 seconds and keeps looping through updates instead of stopping at the first IN_FLIGHT message, giving proper exponential-backoff behavior.
Review and merge if the change passes existing LND REST wallet tests; verify that a 30-second timeout does not break legitimate long-polling behavior on slow networks; consider adding a test that simulates an IN_FLIGHT stream followed by SUCCEEDED/FAILED to ensure the loop terminates correctly.
Security signals we found
Denial-of-service resource exhaustion: unbounded streaming connections can accumulate and exhaust HTTP client connections, file descriptors, or memory.
State-handling correctness: returning pending on the first IN_FLIGHT event could mask final terminal states and affect payment accounting reliability.
Timeout hardening: adding a finite timeout bounds worst-case resource consumption per poll.
No input validation changes or cryptographic fixes present.
Evidence from the diff
In lnbits/wallets/lndrest.py the LndRestWallet.get_payment_status() method streams /v2/router/track/{checking_id}. The patch changes timeout=None to timeout=30 and changes the IN_FLIGHT branch from returning PaymentPendingStatus() to continue. The 30-second timeout prevents indefinite HTTP/2 or SSE-like streams, while continuing on IN_FLIGHT lets the caller’s outer retry/backoff logic observe intermediate states and eventually reach a terminal SUCCEEDED/FAILED/IN_FLIGHT conclusion. This aligns the implementation with the PR title’s intent of exponential backoff for in-flight payment polling.
Changed components
lnbits/wallets/lndrest.pyLndRestWallet.get_payment_status()LND REST /v2/router/track/{payment_hash} streaming endpoint integrationInspect captured patch +2 / −2
diff --git a/lnbits/wallets/lndrest.py b/lnbits/wallets/lndrest.py
index c5eb198..0848a7a 100644
--- a/lnbits/wallets/lndrest.py
+++ b/lnbits/wallets/lndrest.py
@@ -243,7 +243,7 @@ class LndRestWallet(Wallet):
return PaymentPendingStatus()
url = f"/v2/router/track/{checking_id}"
- async with self.client.stream("GET", url, timeout=None) as r:
+ async with self.client.stream("GET", url, timeout=30) as r:
async for json_line in r.aiter_lines():
try:
line = json.loads(json_line)
@@ -274,7 +274,7 @@ class LndRestWallet(Wallet):
return PaymentFailedStatus()
elif status == "IN_FLIGHT":
logger.info(f"LNDRest Payment in flight: {checking_id}")
- return PaymentPendingStatus()
+ continue
logger.info(f"LNDRest Payment non-existent: {checking_id}")
return PaymentPendingStatus()
Why this scored 35/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.