3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 14:54:15 +00:00

DnsDiscovery: attempt workaround for apparent lack of working thread safety on some Linux platform C libraries.

This commit is contained in:
Mike Hearn 2015-01-28 16:21:40 +01:00
parent ad4fb5103c
commit fe2aff49ae
2 changed files with 19 additions and 6 deletions

View File

@ -17,11 +17,10 @@
package org.bitcoinj.net.discovery; package org.bitcoinj.net.discovery;
import org.bitcoinj.core.NetworkParameters; import org.bitcoinj.core.*;
import org.bitcoinj.utils.*;
import java.net.InetAddress; import java.net.*;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -63,6 +62,16 @@ public class DnsDiscovery extends MultiplexingDiscovery {
return discoveries; return discoveries;
} }
@Override
protected ExecutorService createExecutor() {
// Attempted workaround for reported bugs on Linux in which gethostbyname does not appear to be properly
// thread safe and can cause segfaults on some libc versions.
if (System.getProperty("os.name").toLowerCase().contains("linux"))
return Executors.newSingleThreadExecutor(new DaemonThreadFactory());
else
return Executors.newFixedThreadPool(seeds.size(), new DaemonThreadFactory());
}
/** Implements discovery from a single DNS host. */ /** Implements discovery from a single DNS host. */
public static class DnsSeedDiscovery implements PeerDiscovery { public static class DnsSeedDiscovery implements PeerDiscovery {
private final String hostname; private final String hostname;

View File

@ -33,7 +33,7 @@ import static com.google.common.base.Preconditions.checkArgument;
/** /**
* MultiplexingDiscovery queries multiple PeerDiscovery objects, shuffles their responses and then returns the results, * MultiplexingDiscovery queries multiple PeerDiscovery objects, shuffles their responses and then returns the results,
* thus selecting randomly between them and reducing the influence of any particular seed. Any that don't respond * thus selecting randomly between them and reducing the influence of any particular seed. Any that don't respond
* within the timeout are ignored. Backends are queried in parallel. Backends may block * within the timeout are ignored. Backends are queried in parallel. Backends may block.
*/ */
public class MultiplexingDiscovery implements PeerDiscovery { public class MultiplexingDiscovery implements PeerDiscovery {
private static final Logger log = LoggerFactory.getLogger(MultiplexingDiscovery.class); private static final Logger log = LoggerFactory.getLogger(MultiplexingDiscovery.class);
@ -53,7 +53,7 @@ public class MultiplexingDiscovery implements PeerDiscovery {
@Override @Override
public InetSocketAddress[] getPeers(final long timeoutValue, final TimeUnit timeoutUnit) throws PeerDiscoveryException { public InetSocketAddress[] getPeers(final long timeoutValue, final TimeUnit timeoutUnit) throws PeerDiscoveryException {
vThreadPool = Executors.newFixedThreadPool(seeds.size(), new DaemonThreadFactory()); vThreadPool = createExecutor();
try { try {
List<Callable<InetSocketAddress[]>> tasks = Lists.newArrayList(); List<Callable<InetSocketAddress[]>> tasks = Lists.newArrayList();
for (final PeerDiscovery seed : seeds) { for (final PeerDiscovery seed : seeds) {
@ -93,6 +93,10 @@ public class MultiplexingDiscovery implements PeerDiscovery {
} }
} }
protected ExecutorService createExecutor() {
return Executors.newFixedThreadPool(seeds.size(), new DaemonThreadFactory());
}
@Override @Override
public void shutdown() { public void shutdown() {
ExecutorService tp = vThreadPool; ExecutorService tp = vThreadPool;