Fix RemoteClusterConnection close race (#45898)

Closing a `RemoteClusterConnection` concurrently with trying to connect
could result in double invoking the listener.

This fixes
RemoteClusterConnectionTest#testCloseWhileConcurrentlyConnecting

Closes #45845
This commit is contained in:
Henning Andersen 2019-08-23 14:25:26 +02:00 committed by Henning Andersen
parent 8e66df9925
commit 46d9a575db
2 changed files with 4 additions and 3 deletions

View File

@ -368,8 +368,10 @@ final class RemoteClusterConnection implements TransportConnectionListener, Clos
boolean runConnect = false; boolean runConnect = false;
final ActionListener<Void> listener = final ActionListener<Void> listener =
ContextPreservingActionListener.wrapPreservingContext(connectListener, threadPool.getThreadContext()); ContextPreservingActionListener.wrapPreservingContext(connectListener, threadPool.getThreadContext());
boolean closed;
synchronized (mutex) { synchronized (mutex) {
if (closed.get()) { closed = this.closed.get();
if (closed) {
assert listeners.isEmpty(); assert listeners.isEmpty();
} else { } else {
if (listeners.size() >= MAX_LISTENERS) { if (listeners.size() >= MAX_LISTENERS) {
@ -382,7 +384,7 @@ final class RemoteClusterConnection implements TransportConnectionListener, Clos
runConnect = listeners.size() == 1; runConnect = listeners.size() == 1;
} }
} }
if (closed.get()) { if (closed) {
connectListener.onFailure(new AlreadyClosedException("connect handler is already closed")); connectListener.onFailure(new AlreadyClosedException("connect handler is already closed"));
return; return;
} }

View File

@ -655,7 +655,6 @@ public class RemoteClusterConnectionTests extends ESTestCase {
} }
} }
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/45845")
public void testCloseWhileConcurrentlyConnecting() throws IOException, InterruptedException, BrokenBarrierException { public void testCloseWhileConcurrentlyConnecting() throws IOException, InterruptedException, BrokenBarrierException {
List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>(); List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT); try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT);