skip the keepalive ping while another request is awaiting its response
What changed, and why it matters
This change stops Sparrow Wallet from sending unnecessary 'are you still there?' keepalive pings when another request is already waiting for an answer. On some Electrum server backends (notably electrs), a ping would queue up behind the pending request and waste a full synchronization cycle. The fix improves responsiveness and reduces wasted network round-trips, but it is not a direct fix for a remote exploit or data breach.
No immediate security action required. Treat as a normal reliability/performance improvement. Users and downstream packagers can update at their usual cadence. If this change is backported, ensure the new TcpTransport.isRequestPending() method and its unit test are included to avoid behavioral inconsistency.
Security signals we found
Avoidance of redundant keepalive pings that could delay synchronization
New transport-level predicate exposing in-flight request state
Unit test added to assert correct pending-request lifecycle
Evidence from the diff
ElectrumServer.ping() now checks whether the underlying TcpTransport already has a request in flight via a new isRequestPending() method (which returns clientRequestLock.isLocked()). If a request is pending, the keepalive ping is skipped because the in-flight request itself demonstrates server liveness. A unit test verifies that isRequestPending() is true while a request is awaiting its response and false afterward. The change is a performance/reliability optimization, not a patch for a cryptographic or remote-code-execution vulnerability.
Changed components
com.sparrowwallet.sparrow.net.ElectrumServercom.sparrowwallet.sparrow.net.TcpTransportElectrum server keepalive/ping logicInspect captured patch +50 / −1
### src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
@@ -298,7 +298,12 @@ public void connect() throws ServerException {
}
public void ping() throws ServerException {
- electrumServerRpc.ping(getTransport());
+ //A request awaiting its response already shows whether the server is still there, and a ping would wait behind it,
+ //costing a full sync cycle on a server that answers requests in turn, as electrs does
+ CloseableTransport closeableTransport = getTransport();
+ if(!(closeableTransport instanceof TcpTransport tcpTransport && tcpTransport.isRequestPending())) {
+ electrumServerRpc.ping(closeableTransport);
+ }
}
public List<String> getServerVersion() throws ServerException {
### src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java
@@ -433,6 +433,10 @@ public void close() throws IOException {
}
}
+ public boolean isRequestPending() {
+ return clientRequestLock.isLocked();
+ }
+
@Override
public int getTimeoutCount() {
return timeoutCount;
### src/test/java/com/sparrowwallet/sparrow/net/TcpTransportTimeoutTest.java
@@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -146,6 +147,45 @@ public void slowResponseIsWaitedForWithoutResending() throws Exception {
}
}
+ @Test
+ public void requestIsPendingUntilItsResponseArrives() throws Exception {
+ try(ServerSocket serverSocket = createServerSocket()) {
+ TcpTransport transport = connectTransport(serverSocket);
+ CountDownLatch firstReplyGate = new CountDownLatch(1);
+ startServer(serverSocket, firstReplyGate);
+
+ try {
+ assertFalse(transport.isRequestPending());
+
+ AtomicReference<Object> result = new AtomicReference<>();
+ Thread requestThread = new Thread(() -> {
+ try {
+ result.set(transport.pass(pingRequest(1)));
+ } catch(IOException e) {
+ result.set(e);
+ }
+ });
+ requestThread.setDaemon(true);
+ requestThread.start();
+
+ assertTimeoutPreemptively(Duration.ofSeconds(5), () -> {
+ while(!transport.isRequestPending()) {
+ Thread.onSpinWait();
+ }
+ });
+
+ firstReplyGate.countDown();
+ requestThread.join(Duration.ofSeconds(transport.readTimeouts[0] + 5));
+ assertFalse(requestThread.isAlive(), "The request must complete once its reply is released");
+ assertEquals(pingResponse(1), result.get(), "The request must return its reply rather than fail");
+ assertFalse(transport.isRequestPending(), "No request may be pending once its response has arrived");
+ } finally {
+ firstReplyGate.countDown();
+ transport.close();
+ }
+ }
+ }
+
@Test
public void responseSplitBySocketReadTimeoutIsReassembled() throws Exception {
try(ServerSocket serverSocket = createServerSocket()) {Why this scored 26/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.