Skip to content

Commit

Permalink
Fix potential race during TcpTransport close
Browse files Browse the repository at this point in the history
Simplify locking around serverChannels a bit.

Relates to CI failure issue: elastic#37543
  • Loading branch information
henningandersen committed Feb 18, 2019
1 parent e1b5047 commit 7de5032
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions server/src/main/java/org/elasticsearch/transport/TcpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,23 +388,16 @@ private InetSocketAddress bindToPort(final String name, final InetAddress hostAd
PortsRange portsRange = new PortsRange(port);
final AtomicReference<Exception> lastException = new AtomicReference<>();
final AtomicReference<InetSocketAddress> boundSocket = new AtomicReference<>();
closeLock.readLock().lock();
closeLock.writeLock().lock();
try {
if (lifecycle.initialized() == false && lifecycle.started() == false) {
throw new IllegalStateException("transport has been stopped");
}
boolean success = portsRange.iterate(portNumber -> {
try {
TcpServerChannel channel = bind(name, new InetSocketAddress(hostAddress, portNumber));
synchronized (serverChannels) {
List<TcpServerChannel> list = serverChannels.get(name);
if (list == null) {
list = new ArrayList<>();
serverChannels.put(name, list);
}
list.add(channel);
boundSocket.set(channel.getLocalAddress());
}
serverChannels.computeIfAbsent(name, k -> new ArrayList<>()).add(channel);
boundSocket.set(channel.getLocalAddress());
} catch (Exception e) {
lastException.set(e);
return false;
Expand All @@ -415,7 +408,7 @@ private InetSocketAddress bindToPort(final String name, final InetAddress hostAd
throw new BindTransportException("Failed to bind to [" + port + "]", lastException.get());
}
} finally {
closeLock.readLock().unlock();
closeLock.writeLock().unlock();
}
if (logger.isDebugEnabled()) {
logger.debug("Bound profile [{}] to address {{}}", name, NetworkAddress.format(boundSocket.get()));
Expand Down

0 comments on commit 7de5032

Please sign in to comment.