AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Moderate 50 Monero

fix: Electrum wallets stuck on Connecting after a failed connect (#3763)

Public commit record

What the developer wrote

Authored by Omar Hatem

100/100 · Strong
fix: 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>
✓ 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
The short version

What changed, and why it matters

This 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.

Recommended action

Treat this as a reliability and minor security hardening fix. It primarily prevents denial-of-service-like UX failures and resource leaks rather than enabling remote code execution or theft. Reviewers should verify the new timeouts and the _connectAttempt/_isClosed guards cover all async paths, and that the added tests pass. Users on affected versions should update to avoid stuck wallets and missed transaction notifications.

Security signals we found

01

Connection-state confusion: failed connects were not reported, leaving the wallet in a 'Connecting' state indefinitely and preventing automatic recovery.

02

Missing timeout on TLS/SOCKS handshakes allowed connections to hang forever, blocking user-initiated node switches and Tor toggles.

03

Race between overlapping connection attempts: a late socket from a superseded attempt could overwrite the active socket or flip useSSL, and late onDone/onError could tear down the successor socket.

04

Late replies on a replaced socket could complete same-id requests of the new session because request IDs restart at 0 after close().

05

Resource leaks: SOCKS failure did not close the Tor proxy connection; the 5 s reconnect timer was never cancelled, so closed wallets could reopen their Electrum client and leave sockets/keep-alive timers orphaned.

06

Missed subscriptions after reconnect: the script-hash subscription map was only cleared on a 'disconnected' event, so node switches/Tor toggles/resume reconnects did not re-subscribe addresses, breaking push notifications for incoming payments.

07

False 'Synced' state: app-resume reconnection could call startSync() on a failed client, syncing a dead client into a false positive.

Risk score

Why this scored 50/100

Our methodology →
Potential impact 12/30
Exploitability 8/25
Stealth signal 9/15
Affected reach 10/15
Confidence 7/10
Evidence quality 4/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.