Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give LocalBitcoinNode a timeout in case BitcoinJ hangs during handshake attempt #4058

Merged
merged 2 commits into from
Mar 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/src/main/java/bisq/core/btc/nodes/LocalBitcoinNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -49,6 +51,7 @@ public class LocalBitcoinNode {

private static final Logger log = LoggerFactory.getLogger(LocalBitcoinNode.class);
private static final int CONNECTION_TIMEOUT = 5000;
private static final int HANDSHAKE_TIMEOUT = CONNECTION_TIMEOUT;

private final Config config;
private final int port;
Expand Down Expand Up @@ -244,10 +247,15 @@ private Optional<VersionMessage> attemptHandshakeForVersionMessage() {

// block for VersionMessage or cancellation (in case of connection failure)
try {
var peerVersionMessage = peerVersionMessageFuture.get();
var peerVersionMessage = peerVersionMessageFuture.get(HANDSHAKE_TIMEOUT, TimeUnit.MILLISECONDS);
optionalPeerVersionMessage = Optional.of(peerVersionMessage);
} catch (ExecutionException | InterruptedException | CancellationException ex) {
optionalPeerVersionMessage = Optional.empty();
} catch (TimeoutException ex) {
optionalPeerVersionMessage = Optional.empty();
log.error("Exploratory handshake attempt with a local Bitcoin node (that may not be there)" +
" unexpectedly timed out. This should never happen; please report this. HANDSHAKE_TIMEOUT" +
" is {} ms. Continuing as if a local BTC node was not found.", HANDSHAKE_TIMEOUT);
}

peer.close();
Expand Down