fix(nwc): resolve in-flight request futures when relay disconnects (#4161)
What changed, and why it matters
This patch fixes a bug in LNbits' Nostr Wallet Connect (NWC) wallet integration where requests waiting for a response could hang forever if the relay connection dropped. Previously, the timeout cleanup loop skipped work while disconnected, so old requests never failed and callers would wait indefinitely, requiring a full process restart to recover. The fix makes the timeout loop run even when disconnected and resolves those waiting futures with a timeout error instead of leaving them stuck.
Treat as a reliability/availability hardening fix. Review whether other wallet backends or connection managers have similar patterns where cleanup loops skip work while disconnected, leaving futures unresolved. No immediate security patch urgency beyond normal update cadence unless NWC availability is critical to deployment.
Security signals we found
Denial-of-service / availability impact: unbounded caller waits leading to resource exhaustion and requiring process restart
Asynchronous future leak: in-flight subscriptions never resolved on connection drop
Timeout cleanup bypassed during disconnected state
No authentication bypass, code execution, or data leakage signals in diff
Evidence from the diff
In lnbits/wallets/nwc.py, the subscription timeout handler previously continued its loop without processing subscriptions when self.connected was false. Because futures associated with in-flight subscriptions were only resolved by incoming events or timeout cleanup, a dropped connection meant those futures were never completed. The patch removes the early continue, processes timeouts regardless of connection state, and passes send_event=self.connected to _close_subscription_by_subid so that CLOSE messages are only sent when connected, while local cleanup still occurs. This resolves hanging awaitables and avoids blocking on _wait_for_connection during disconnects.
Changed components
lnbits/wallets/nwc.pyNWCConnection subscription timeout handlerNostr Wallet Connect wallet backendInspect 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 43/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.