fix(nwc): resolve in-flight request futures when relay disconnects (#4161)
What changed, and why it matters
This change fixes a bug in LNbits' Nostr Wallet Connect (NWC) integration where pending payment or invoice requests could hang forever if the relay connection dropped. The fix ensures these waiting requests are properly marked as failed with a timeout when the connection is down, instead of leaving them stuck until the server is restarted. It is a reliability/availability fix rather than a direct theft-of-funds vulnerability, but the hang could be abused to deny service or confuse automated services.
Treat as a medium-priority reliability/availability fix. Deploy the patch and monitor NWC relay disconnect/reconnect behavior. Consider adding metrics or tests for in-flight request cleanup during relay outages. No immediate incident response is required unless unexplained hangs have already been observed.
Security signals we found
Denial-of-service via resource exhaustion: unbounded accumulation of awaiting futures
Reliability issue in wallet backend could block payment/invoice operations
Fix explicitly notes 'process restart to recover' indicating availability impact
No input validation, authentication, or cryptographic changes observed
Evidence from the diff
In lnbits/wallets/nwc.py, the subscription timeout loop previously skipped cleanup entirely when self.connected was false. If the WebSocket to the NWC relay disconnected while requests were in flight, their asyncio.Future objects were never resolved because the old subscriptions are not reused after reconnection. The patch removes the if not self.connected: continue guard, resolves timed-out futures with an exception even while disconnected, and passes send_event=self.connected to _close_subscription_by_subid so the local cleanup does not block waiting for a reconnection. This prevents coroutines from awaiting forever and avoids a process restart to recover.
Changed components
lnbits/wallets/nwc.pyNWCConnection subscription timeout handlerNostr Wallet Connect relay connection lifecycleInspect captured patch +13 / −6
diff --git a/lnbits/wallets/nwc.py b/lnbits/wallets/nwc.py
index a875f39..7fde75b 100644
--- a/lnbits/wallets/nwc.py
+++ b/lnbits/wallets/nwc.py
@@ -949,10 +949,12 @@ class NWCConnection:
while not self._is_shutting_down():
try:
await asyncio.sleep(int(self.subscription_timeout * 0.5))
- # skip if connection is not established
- if not self.connected:
- continue
- # Find all subscriptions that have timed out
+ # Find all subscriptions that have timed out.
+ # This must run even while the connection is down:
+ # otherwise in-flight request futures on a dropped
+ # connection are never resolved and callers await forever
+ # (the socket can reconnect but the old subscriptions never
+ # complete), which requires a process restart to recover.
now = time.time()
subscriptions_to_close = []
for subscription in self.subscriptions.values():
@@ -970,9 +972,14 @@ class NWCConnection:
subscription["future"].set_exception(
Exception("timed out")
)
- # Close all timed out subscriptions
+ # Close all timed out subscriptions. When the connection is
+ # down, drop the stale entries locally instead of sending
+ # CLOSE (which blocks on _wait_for_connection); the futures
+ # are already resolved above so callers do not hang.
for sub_id in subscriptions_to_close:
- await self._close_subscription_by_subid(sub_id)
+ await self._close_subscription_by_subid(
+ sub_id, send_event=self.connected
+ )
except Exception as e:
logger.error("Error handling subscription timeout: " + str(e))
except Exception as e:
Why this scored 42/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.