create the server transport only when connecting, so a call made after disconnecting cannot leave one for the next connection
What changed, and why it matters
This commit fixes a logic bug in how Sparrow Wallet connects to Bitcoin Electrum servers. Previously, background tasks that ran after a disconnect could accidentally create a new server connection on their own, using stale or mismatched settings, instead of using the proper connection lifecycle. The change ensures that only the official 'connect()' routine can create a transport, and any late-running task simply fails with a 'not connected' error rather than silently opening its own connection.
Treat as a bug-fix with security-hardening value. Review whether any other code paths still lazily create transports or network resources outside connect(), and verify that isConnected() checks are race-free. No immediate emergency response is indicated, but users should upgrade to a release containing this commit.
Security signals we found
Lifecycle bypass: background code paths could instantiate a network transport outside the intended connection sequence
State mismatch risk: a transport created post-disconnect would use settings captured at creation time, not at the moment the user initiated the connection
Defense in depth: change converts a silent auto-create into an explicit exception, preventing unintended reuse
Commentary in diff explicitly frames the change as preventing a call after disconnect from leaving a transport for the next connection
Evidence from the diff
ElectrumServer.java previously exposed a single getTransport() helper that lazily created a CloseableTransport whenever one was absent. Background tasks such as syncAnnouncedHeaders() and merkle-proof fetches could call it after a disconnect; that would open a transport outside the normal connect()/disconnect() lifecycle and using whatever Server settings were current at that moment. The patch splits the behavior: getTransport() now only returns an existing transport or throws, while a new private getConnectionTransport() is the sole path that creates a transport and is only called from connect().
Changed components
src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.javaElectrum server connection managementBackground header/merkle-proof synchronization tasksInspect captured patch +13 / −4
### src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
@@ -200,6 +200,15 @@ public class ElectrumServer {
private static final Set<Sha256Hash> unsignedWarnedTxids = ConcurrentHashMap.newKeySet();
private static synchronized CloseableTransport getTransport() throws ServerException {
+ if(transport == null) {
+ throw new ServerException("Not connected to a server");
+ }
+
+ return transport;
+ }
+
+ //Only a connection creates the transport, from the settings of that moment, so that one created by a call made after a disconnection cannot be reused
+ private static synchronized CloseableTransport getConnectionTransport() throws ServerException {
if(transport == null) {
try {
Server electrumServer = null;
@@ -284,7 +293,7 @@ static void clearPreviousServerState() {
}
public void connect() throws ServerException {
- CloseableTransport closeableTransport = getTransport();
+ CloseableTransport closeableTransport = getConnectionTransport();
closeableTransport.connect();
}
@@ -3781,7 +3790,7 @@ private void refuseAnnouncedTip(ChainTip tip, Exception e) throws ServerExceptio
/**
* The body of a run, which happens on a background thread: the connection check is therefore the transport level one, since AppServices reads
* a JavaFX Service and may only be called from the application thread. Without the check a retry firing after the connection closed would
- * have getTransport() open a transport of its own, outside the connection lifecycle.
+ * fail for want of a transport, and be taken as the server failing to substantiate the tip it announced.
*/
static void syncAnnouncedHeaders(ChainTip tip) throws ServerException {
if(!isConnected()) {
@@ -4183,7 +4192,7 @@ protected Task<Map<String, TransactionMerkleProof>> createTask() {
@Override
protected Map<String, TransactionMerkleProof> call() {
try {
- //getTransport() opens one where there is none, so a task running after the connection closed must not reach it
+ //A task running after the connection closed has no transport to fetch with
if(isConnected()) {
return electrumServerRpc.getTransactionMerkleProofs(getTransport(), null, List.of(reference));
}
@@ -4218,7 +4227,7 @@ protected Task<BlockHeader> createTask() {
return new Task<>() {
@Override
protected BlockHeader call() throws ServerException {
- //getTransport() opens one where there is none, so a task running after the connection closed must not reach it
+ //A task running after the connection closed has no transport to fetch with
if(!isConnected()) {
throw new ServerException("Not connected");
}Why this scored 48/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.