Skip to content

Commit 92221eb

Browse files
committed
Fix closing uninitialized channel pools
1 parent 250cb9d commit 92221eb

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

core/src/main/java/com/datastax/oss/driver/internal/core/pool/ChannelPool.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,13 @@ private CompletionStage<Boolean> reconnect() {
418418
} else {
419419
resultFuture.complete(false);
420420
}
421+
} else if (isClosing) {
422+
LOG.debug(
423+
"[{}] New channel added ({}) but the pool was closed, closing it",
424+
logPrefix,
425+
driver);
426+
driver.forceClose();
427+
resultFuture.complete(true);
421428
} else {
422429
initialize(driver);
423430
CompletableFutures.completeFrom(addMissingChannels(), resultFuture);

core/src/test/java/com/datastax/oss/driver/internal/core/pool/ChannelPoolShutdownTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,40 @@ public void should_close_all_channels_when_closed() throws Exception {
104104
factoryHelper.verifyNoMoreCalls();
105105
}
106106

107+
@Test
108+
public void should_force_close_first_reconnected_channel_when_closed_before_initialized()
109+
throws Exception {
110+
when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
111+
when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(1);
112+
113+
DriverChannel channel1 = newMockDriverChannel(1);
114+
CompletableFuture<DriverChannel> channel1Future = new CompletableFuture<>();
115+
MockChannelFactoryHelper factoryHelper =
116+
MockChannelFactoryHelper.builder(channelFactory)
117+
// init
118+
.failure(node, "mock channel init failure")
119+
// reconnection
120+
.pending(node, channel1Future)
121+
.build();
122+
123+
CompletionStage<ChannelPool> poolFuture =
124+
ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
125+
126+
factoryHelper.waitForCalls(node, 2);
127+
assertThatStage(poolFuture).isSuccess();
128+
ChannelPool pool = poolFuture.toCompletableFuture().get();
129+
130+
CompletionStage<Void> closeFuture = pool.closeAsync();
131+
assertThatStage(closeFuture).isSuccess();
132+
133+
channel1Future.complete(channel1);
134+
135+
verify(channel1, VERIFY_TIMEOUT).forceClose();
136+
verify(eventBus, never()).fire(ChannelEvent.channelOpened(node));
137+
138+
factoryHelper.verifyNoMoreCalls();
139+
}
140+
107141
@Test
108142
public void should_force_close_all_channels_when_force_closed() throws Exception {
109143
when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
@@ -170,4 +204,38 @@ public void should_force_close_all_channels_when_force_closed() throws Exception
170204

171205
factoryHelper.verifyNoMoreCalls();
172206
}
207+
208+
@Test
209+
public void should_force_close_first_reconnected_channel_when_force_closed_before_initialized()
210+
throws Exception {
211+
when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
212+
when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(1);
213+
214+
DriverChannel channel1 = newMockDriverChannel(1);
215+
CompletableFuture<DriverChannel> channel1Future = new CompletableFuture<>();
216+
MockChannelFactoryHelper factoryHelper =
217+
MockChannelFactoryHelper.builder(channelFactory)
218+
// init
219+
.failure(node, "mock channel init failure")
220+
// reconnection
221+
.pending(node, channel1Future)
222+
.build();
223+
224+
CompletionStage<ChannelPool> poolFuture =
225+
ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
226+
227+
factoryHelper.waitForCalls(node, 2);
228+
assertThatStage(poolFuture).isSuccess();
229+
ChannelPool pool = poolFuture.toCompletableFuture().get();
230+
231+
CompletionStage<Void> closeFuture = pool.forceCloseAsync();
232+
assertThatStage(closeFuture).isSuccess();
233+
234+
channel1Future.complete(channel1);
235+
236+
verify(channel1, VERIFY_TIMEOUT).forceClose();
237+
verify(eventBus, never()).fire(ChannelEvent.channelOpened(node));
238+
239+
factoryHelper.verifyNoMoreCalls();
240+
}
173241
}

0 commit comments

Comments
 (0)