From 158ecd4ab192918eaddd66cb6867843fa0520a85 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 24 Oct 2023 10:51:09 +0200 Subject: [PATCH] fix thread race issue when connecting to cormorant electrum server --- .../sparrowwallet/sparrow/net/TcpTransport.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java b/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java index cbf17515..e3a10856 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java @@ -1,6 +1,5 @@ package com.sparrowwallet.sparrow.net; -import com.github.arteam.simplejsonrpc.client.Transport; import com.github.arteam.simplejsonrpc.server.JsonRpcServer; import com.google.common.base.Splitter; import com.google.common.net.HostAndPort; @@ -19,6 +18,7 @@ import java.net.SocketTimeoutException; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Objects; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; @@ -40,6 +40,8 @@ public class TcpTransport implements CloseableTransport, TimeoutCounter { private String response; + private final CountDownLatch readReadySignal = new CountDownLatch(1); + private final ReentrantLock readLock = new ReentrantLock(); private final Condition readingCondition = readLock.newCondition(); @@ -110,6 +112,17 @@ public class TcpTransport implements CloseableTransport, TimeoutCounter { } private String readResponse() throws IOException { + if(firstRead) { + try { + //Ensure read thread has started + if(!readReadySignal.await(2, TimeUnit.SECONDS)) { + throw new IOException("Read thread did not start"); + } + } catch(InterruptedException e) { + throw new IOException("Read ready await interrupted"); + } + } + try { if(!readLock.tryLock((readTimeouts[readTimeoutIndex] * 1000L) + (requestIdCount * PER_REQUEST_READ_TIMEOUT_MILLIS), TimeUnit.MILLISECONDS)) { readTimeoutIndex = Math.min(readTimeoutIndex + 1, readTimeouts.length - 1); @@ -155,6 +168,7 @@ public class TcpTransport implements CloseableTransport, TimeoutCounter { public void readInputLoop() throws ServerException { readLock.lock(); + readReadySignal.countDown(); try { try {