Commit message · Omar Hatemfix: Electrum wallets stuck on Connecting after a failed connect (#3763)
* fix: Electrum wallets stuck on Connecting after a failed connect
One failed connect left BTC/LTC/BCH/DOGE wallets on the Connecting
spinner until the app was restarted, and switching nodes or toggling
Tor did nothing. connect() set `connecting` before its `failed` checks,
so failures were never reported; the wallet's 5 s reconnect only
handles NotConnected/LostConnection; and connectToNode returned early
while Connecting. A stalled TLS or SOCKS handshake had no timeout, so
it never even reached the failure path.
- ElectrumClient.connect always reports `failed`, so the existing
retry takes over. A per-attempt counter, bumped by connect() and
close(), keeps a superseded or closed attempt from installing its
socket, reporting status or flipping useSSL; its late socket is
destroyed.
- ping() no longer reports `connected` when there is no socket
(callWithTimeout returns null instead of throwing), which flipped a
dropped connection back to Connected and stopped the reconnect.
Replies from a replaced socket are ignored.
- A replaced socket's late onDone/onError no longer destroys its
successor.
- ProxySocket.connect bounds the whole connect, SOCKS and TLS
handshakes included: 20 s on clearnet, 60 s over Tor. Sockets that
connect after the deadline are destroyed and a timed-out SOCKS
session is closed. Node reachability checks share this path, so a
hung check now fails at the deadline instead of never finishing.
- connectToNode only skips an exact repeat of the attempt in flight
(same node, same Tor state).
- The app-resume reconnect skips startSync() when the connect failed,
instead of syncing a dead client into a false "Synced".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix: re-subscribe addresses after switching Electrum nodes
The previous commit stops an old socket's late close from reporting
"disconnected". That event was what reset the wallet's script-hash
subscription map on a node switch, Tor toggle or app-resume reconnect,
so the new server never got blockchain.scripthash.subscribe and incoming
payments were not pushed. Reset the map wherever the client is closed
for a reconnect.
Also correct two comments, and add tests for each connection case,
run against the real ElectrumClient and ElectrumWallet with local fake
servers.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix: close failed Tor sessions and stop retries after wallet close
Now that failed connects are retried every 5 s, two leaks that used to
happen once would repeat:
- When Tor answers the SOCKS handshake with a failure, socks_socket
throws without closing its connection to Tor. Close it before
rethrowing.
- The 5 s reconnect timer was never cancelled, so it could reopen the
client of a wallet that had already been closed (e.g. after switching
wallets). Keep it and cancel it in close().
Also test that the app-resume reconnect re-subscribes the addresses.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix: don't reconnect an Electrum wallet closed mid-sync
Cancelling the retry timer in close() only covered an idle wallet. A
sync still running against the closed client ends in Failed or Lost,
which scheduled a new retry and reopened the client. Mark the wallet
closed and skip scheduling retries for it.
Also fix the resume-path comment: a sync with no socket fails, it does
not end in "Synced" as previously claimed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix: ignore late replies on a replaced Electrum socket and reconnects of a closed wallet
Two guards from review, in code the previous commits already touch:
- A replaced socket's data handler gets the same identity check as its
onDone/onError. A clearnet socket still delivers replies after close()
half-closes it, and close() restarts request ids at 0, so a late reply
from the old server could complete a same-id request of the new session.
- connectToNode() and the app-resume reconnect return early on a closed
wallet, before and after the client close they await. The LTC/BCH/DOGE
connectivity check captures the wallet and awaits a node probe before
calling connectToNode(); a wallet switch during that probe closed the
wallet and then reconnected it, leaving a socket and a keep-alive timer
with no owner.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
100/100 · StrongMessage clarity
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Uses a recognizable type or scope✓ Provides detailed explanatory context✓ Mentions testing or verification✓ Links an issue, advisory, or supporting reference
Why it was queuedsigning or wallet pathsecond-pass: broader security terminologysecond-pass: security-sensitive path
AI analysis · Moderate 50/100This commit fixes a bug in Cake Wallet where Bitcoin-family wallets (BTC, LTC, BCH, DOGE) could get stuck showing 'Connecting' forever after a failed network connection. The root causes were: failed connections were not reported, retries only handled some disconnect cases, and slow or hanging network handshakes had no timeout. The patch makes failures visible, adds timeouts, prevents old/replaced connections from interfering with new ones, and stops closed wallets from reconnecting on their own. It also fixes a related issue where switching servers could stop incoming payment notifications because address subscriptions were not re-sent.